CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

Resource--Test.php
Go to the documentation of this file.
1 <?PHP
2
3 class Resource_Test extends PHPUnit_Framework_TestCase
4 {
5 protected static $TestFieldIds;
6 protected static $TestFields;
7
14 public static function setUpBeforeClass()
15 {
16 # construct the schema object
17 $Schema = new MetadataSchema(MetadataSchema::SCHEMAID_DEFAULT);
18
19 self::$TestFieldIds = array();
20
21 # outline fields to be created
22 self::$TestFields = array(
23 "TestTextField" => MetadataSchema::MDFTYPE_TEXT,
24 "TestTimestampField" => MetadataSchema::MDFTYPE_TIMESTAMP,
25 "TestParagraphField" => MetadataSchema::MDFTYPE_PARAGRAPH,
26 "TestUrlField" => MetadataSchema::MDFTYPE_URL,
27 "TestReferenceField" => MetadataSchema::MDFTYPE_REFERENCE,
28 "TestUserField" => MetadataSchema::MDFTYPE_USER,
29 "TestOptionField" => MetadataSchema::MDFTYPE_OPTION,
30 "TestCNameField" => MetadataSchema::MDFTYPE_CONTROLLEDNAME,
31 "TestTreeField" => MetadataSchema::MDFTYPE_TREE,
32 "TestDateField" => MetadataSchema::MDFTYPE_DATE,
33 "TestFlagField" => MetadataSchema::MDFTYPE_FLAG,
34 "TestNumberField" => MetadataSchema::MDFTYPE_NUMBER);
35
36 # create the fields
37 foreach (self::$TestFields as $FieldName => $FieldType)
38 {
39 $TmpField = $Schema->GetItemByName($FieldName);
40 if ($TmpField === NULL)
41 {
42 $TmpField = $Schema->AddField($FieldName, $FieldType);
43 }
44 $TmpField->IsTempItem(FALSE);
45 self::$TestFieldIds[$FieldName] = $TmpField->Id();
46 }
47
48 # Resource::Create() expects a user to be logged in,
49 # so log in an admin user
50 $UFactory = new CWUserFactory();
51 $Users = $UFactory->GetUsersWithPrivileges(
52 PRIV_RESOURCEADMIN, PRIV_COLLECTIONADMIN);
53 $UserIds = array_keys($Users);
54 $AdminUserId = array_pop($UserIds);
55 $AdminUser = new CWUser($AdminUserId);
56 $GLOBALS["G_User"]->Login($AdminUser->Name(), "", TRUE);
57 }
58
63 public static function tearDownAfterClass()
64 {
65 # construct the schema object
66 $Schema = new MetadataSchema();
67 $Database = new Database();
68
69 # drop all of the test fields
70 foreach (self::$TestFieldIds as $FieldName => $FieldId)
71 {
72 $Schema->DropField($FieldId);
73
74 # remove from OAIFieldMappings too
75 $Database->Query("
76 DELETE FROM OAIFieldMappings
77 WHERE SPTFieldId = " . addslashes($FieldId));
78 }
79 }
80
85 public function testGetandSet()
86 {
87 # Create test-specific objects
88 $TestResource = Resource::Create(MetadataSchema::SCHEMAID_DEFAULT);
89 $TestResource->IsTempResource(FALSE);
90 $TestReferenceResource = Resource::Create(MetadataSchema::SCHEMAID_DEFAULT);
91 $TestReferenceResource->IsTempResource(FALSE);
92 $TestClassification = Classification::Create("TestClassification",
93 self::$TestFieldIds['TestTreeField']);
94 $TestControlledName = new ControlledName(NULL, "TestControlledName",
95 self::$TestFieldIds['TestCNameField']);
96 $TestOptionCName = new ControlledName(NULL, "TestOptionCName",
97 self::$TestFieldIds['TestOptionField']);
98
99 # Test get and set for each field
100 foreach (self::$TestFields as $FieldName => $FieldType)
101 {
102 # whether, before testing equivalence, we need to pop the
103 # returned value out of an array
104 $BeforeTestArrayShift = FALSE;
105
106 # if we're testing the object return, this is the object we'll compare it to.
107 unset($TestObject);
108
109 switch ($FieldType)
110 {
111 case MetadataSchema::MDFTYPE_TEXT:
112 $TgtVal = "A test title";
113 break;
114
115 case MetadataSchema::MDFTYPE_URL:
116 $TgtVal = "http://testtesttest.com";
117 break;
118
119 case MetadataSchema::MDFTYPE_PARAGRAPH:
120 $TgtVal = "I am a test paragraph.";
121 break;
122
123 case MetadataSchema::MDFTYPE_NUMBER:
124 $TgtVal = "0";
125 break;
126
127 case MetadataSchema::MDFTYPE_FLAG:
128 $TgtVal = "1";
129 break;
130
131 case MetadataSchema::MDFTYPE_DATE:
132 $TgtVal = date("Y-m-d");
133 $TestObject = new Date(strval($TgtVal));
134 $TestObjectType = 'Date';
135 $TestFunctionName = 'BeginDate';
136 $TestFunctionArguments = NULL;
137 break;
138
139 case MetadataSchema::MDFTYPE_TIMESTAMP:
140 $TgtVal = date("Y-m-d H:i:s", strtotime($TgtVal));
141 break;
142
143 case MetadataSchema::MDFTYPE_TREE:
144 $TgtVal = array();
145 $TgtVal[$TestClassification->Id()] = "TestClassification";
146 $TestObject = $TestClassification;
147 $TestObjectType = 'Classification';
148 $TestFunctionName = 'FullName';
149 $TestFunctionArguments = NULL;
150 $BeforeTestArrayShift = TRUE;
151 break;
152
153 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
154 $TgtVal = array();
155 $TgtVal[$TestControlledName->Id()] = "TestControlledName";
156 $TestObject = $TestControlledName;
157 $TestObjectType = 'ControlledName';
158 $TestFunctionName = 'Name';
159 $TestFunctionArguments = NULL;
160 $BeforeTestArrayShift = TRUE;
161 break;
162
163 case MetadataSchema::MDFTYPE_OPTION:
164 $TgtVal = array();
165 $TgtVal[$TestOptionCName->Id()] = "TestOptionCName";
166 $TestObject = $TestOptionCName;
167 $TestObjectType = 'ControlledName';
168 $TestFunctionName = 'Name';
169 $TestFunctionArguments = NULL;
170 $BeforeTestArrayShift = TRUE;
171 break;
172
173 case MetadataSchema::MDFTYPE_USER:
174 $TestObject = new CWUser(1);
175 $TgtVal = array( 1 => $TestObject->Name() );
176 $TestObjectType = 'CWUser';
177 $TestFunctionName = 'Id';
178 $TestFunctionArguments = NULL;
179 $BeforeTestArrayShift = TRUE;
180 break;
181
182 case MetadataSchema::MDFTYPE_POINT:
183 $TgtVal = array();
184 $TgtVal['X'] = 5;
185 $TgtVal['Y'] = 7;
186 break;
187
188 case MetadataSchema::MDFTYPE_REFERENCE:
189 $TestObject = $TestReferenceResource;
190 $TgtVal = array();
191 $TgtVal[$TestReferenceResource->Id()] = $TestReferenceResource->Id();
192 $TestFunctionName = 'Id';
193 $TestObjectType = 'Resource';
194 $TestFunctionArguments = NULL;
195 $BeforeTestArrayShift = TRUE;
196 break;
197
198 default:
199 throw new Exception("Data type not handled.");
200 break;
201
202 }
203
204 # set the value on the test resource
205 $TestResource->Set($FieldName, $TgtVal);
206
207 # assert the default get returns the expected value
208 $this->assertEquals($TgtVal, $TestResource->Get($FieldName));
209
210 if (isset($TestObject))
211 {
212 $ReturnedObject = $TestResource->Get($FieldName, TRUE);
213
214 if ($BeforeTestArrayShift)
215 {
216 $ReturnedObject = array_shift($ReturnedObject);
217 }
218
219 $array_for_test_object = array($TestObject, $TestFunctionName);
220 $array_for_returned_object = array($ReturnedObject, $TestFunctionName);
221
222 $this->assertEquals(call_user_func(
223 $array_for_returned_object, $TestFunctionArguments),
224 call_user_func($array_for_test_object, $TestFunctionArguments));
225
226 $this->assertInstanceOf($TestObjectType, $ReturnedObject);
227 }
228
229 }
230
231 # Clean up function-specific objects.
232 $TestResource->Delete();
233 $TestReferenceResource->Delete();
234 $TestClassification->Delete();
235 $TestControlledName->Delete();
236 $TestOptionCName->Delete();
237 }
238 }
MetadataSchema\MDFTYPE_TIMESTAMP
const MDFTYPE_TIMESTAMP
Definition: MetadataSchema.php:25
Resource_Test\tearDownAfterClass
static tearDownAfterClass()
After to running the tests, this function is run.
Definition: Resource--Test.php:63
MetadataSchema
Metadata schema (in effect a Factory class for MetadataField).
Definition: MetadataSchema.php:13
Database
SQL database abstraction object with smart query caching.
Definition: Database.php:22
Classification\Create
static Create($Name, $FieldId, $ParentId=NULL)
Add new classification to the hierarchy.
Definition: Classification.php:31
MetadataSchema\MDFTYPE_REFERENCE
const MDFTYPE_REFERENCE
Definition: MetadataSchema.php:35
MetadataSchema\MDFTYPE_NUMBER
const MDFTYPE_NUMBER
Definition: MetadataSchema.php:23
MetadataSchema\MDFTYPE_TREE
const MDFTYPE_TREE
Definition: MetadataSchema.php:27
Date
Definition: Date.php:18
MetadataSchema\MDFTYPE_OPTION
const MDFTYPE_OPTION
Definition: MetadataSchema.php:29
ControlledName
Metadata type representing non-hierarchical controlled vocabulary values.
Definition: ControlledName.php:14
MetadataSchema\MDFTYPE_FLAG
const MDFTYPE_FLAG
Definition: MetadataSchema.php:26
MetadataSchema\MDFTYPE_CONTROLLEDNAME
const MDFTYPE_CONTROLLEDNAME
Definition: MetadataSchema.php:28
Resource_Test\setUpBeforeClass
static setUpBeforeClass()
Prior to running any of the tests, this function is run.
Definition: Resource--Test.php:14
CWUserFactory
CWIS-specific user factory class.
Definition: CWUserFactory.php:13
PHP
PHP
Definition: RSSClient--Test.php:12
MetadataSchema\SCHEMAID_DEFAULT
const SCHEMAID_DEFAULT
Definition: MetadataSchema.php:53
Resource_Test\$TestFields
static $TestFields
Definition: Resource--Test.php:6
MetadataSchema\MDFTYPE_PARAGRAPH
const MDFTYPE_PARAGRAPH
Definition: MetadataSchema.php:22
MetadataSchema\MDFTYPE_DATE
const MDFTYPE_DATE
Definition: MetadataSchema.php:24
MetadataSchema\MDFTYPE_URL
const MDFTYPE_URL
Definition: MetadataSchema.php:33
MetadataSchema\MDFTYPE_TEXT
const MDFTYPE_TEXT
Definition: MetadataSchema.php:21
MetadataSchema\MDFTYPE_USER
const MDFTYPE_USER
Definition: MetadataSchema.php:30
Resource_Test\$TestFieldIds
static $TestFieldIds
Definition: Resource--Test.php:5
Resource\Create
static Create($SchemaId)
Create a new resource.
Definition: Resource.php:47
Resource_Test
Definition: Resource--Test.php:3
MetadataSchema\MDFTYPE_POINT
const MDFTYPE_POINT
Definition: MetadataSchema.php:34
CWUser
CWIS-specific user class.
Definition: CWUser.php:13
Resource_Test\testGetandSet
testGetandSet()
This function exercises the Resource get and set methods for each Metadata types using the fields cre...
Definition: Resource--Test.php:85