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 testGetSetClear()
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(
93 "TestClassification", self::$TestFieldIds['TestTreeField']);
94 $TestControlledName = ControlledName::Create(
95 "TestControlledName", self::$TestFieldIds['TestCNameField']);
96 $TestOptionCName = ControlledName::Create(
97 "TestOptionCName", self::$TestFieldIds['TestOptionField']);
98
99 # Test get and set for each field
100 foreach (self::$TestFieldIds as $FieldName => $FieldId)
101 {
102 $Field = new MetadataField($FieldId);
103
104 # whether, before testing equivalence, we need to pop the
105 # returned value out of an array
106 $BeforeTestArrayShift = FALSE;
107
108 # if we're testing the object return, this is the object we'll compare it to.
109 unset($TestObject);
110
111 switch ($Field->Type())
112 {
113 case MetadataSchema::MDFTYPE_TEXT:
114 $TgtVal = "A test title";
115 break;
116
117 case MetadataSchema::MDFTYPE_URL:
118 $TgtVal = "http://testtesttest.com";
119 break;
120
121 case MetadataSchema::MDFTYPE_PARAGRAPH:
122 $TgtVal = "I am a test paragraph.";
123 break;
124
125 case MetadataSchema::MDFTYPE_NUMBER:
126 $TgtVal = "0";
127 break;
128
129 case MetadataSchema::MDFTYPE_FLAG:
130 $TgtVal = "1";
131 break;
132
133 case MetadataSchema::MDFTYPE_DATE:
134 $TgtVal = date("Y-m-d");
135 $TestObject = new Date(strval($TgtVal));
136 $TestObjectType = 'Date';
137 $TestFunctionName = 'BeginDate';
138 $TestFunctionArguments = NULL;
139 break;
140
141 case MetadataSchema::MDFTYPE_TIMESTAMP:
142 $TgtVal = date("Y-m-d H:i:s");
143 break;
144
145 case MetadataSchema::MDFTYPE_TREE:
146 $TgtVal = array();
147 $TgtVal[$TestClassification->Id()] = "TestClassification";
148 $TestObject = $TestClassification;
149 $TestObjectType = 'Classification';
150 $TestFunctionName = 'FullName';
151 $TestFunctionArguments = NULL;
152 $BeforeTestArrayShift = TRUE;
153 break;
154
155 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
156 $TgtVal = array();
157 $TgtVal[$TestControlledName->Id()] = "TestControlledName";
158 $TestObject = $TestControlledName;
159 $TestObjectType = 'ControlledName';
160 $TestFunctionName = 'Name';
161 $TestFunctionArguments = NULL;
162 $BeforeTestArrayShift = TRUE;
163 break;
164
165 case MetadataSchema::MDFTYPE_OPTION:
166 $TgtVal = array();
167 $TgtVal[$TestOptionCName->Id()] = "TestOptionCName";
168 $TestObject = $TestOptionCName;
169 $TestObjectType = 'ControlledName';
170 $TestFunctionName = 'Name';
171 $TestFunctionArguments = NULL;
172 $BeforeTestArrayShift = TRUE;
173 break;
174
175 case MetadataSchema::MDFTYPE_USER:
176 $UserId = $GLOBALS["G_User"]->Id();
177 $TestObject = new CWUser($UserId);
178 $TgtVal = array( $UserId => $TestObject->Name() );
179 $TestObjectType = 'CWUser';
180 $TestFunctionName = 'Id';
181 $TestFunctionArguments = NULL;
182 $BeforeTestArrayShift = TRUE;
183 break;
184
185 case MetadataSchema::MDFTYPE_POINT:
186 $TgtVal = array();
187 $TgtVal['X'] = 5;
188 $TgtVal['Y'] = 7;
189 break;
190
191 case MetadataSchema::MDFTYPE_REFERENCE:
192 $TestObject = $TestReferenceResource;
193 $TgtVal = array();
194 $TgtVal[$TestReferenceResource->Id()] = $TestReferenceResource->Id();
195 $TestFunctionName = 'Id';
196 $TestObjectType = 'Resource';
197 $TestFunctionArguments = NULL;
198 $BeforeTestArrayShift = TRUE;
199 break;
200
201 default:
202 throw new Exception("Data type not handled.");
203 break;
204
205 }
206
207 # set the value on the test resource
208 $TestResource->Set($Field, $TgtVal);
209
210 # assert the default get returns the expected value
211 $FieldTypeName = StdLib::GetConstantName(
212 "MetadataSchema", $Field->Type(), "MDFTYPE_");
213 $this->assertEquals($TgtVal, $TestResource->Get($Field),
214 "Check that value returned by Get() matches for field type "
215 .$FieldTypeName);
216
217 $RCopy = new Resource($TestResource->Id());
218 $this->assertEquals($TgtVal, $RCopy->Get($Field),
219 "Check that value returned by Get() matches for field type w/ new resource"
220 .$FieldTypeName);
221
222 if (isset($TestObject))
223 {
224 $ReturnedObject = $TestResource->Get($Field, TRUE);
225
226 if ($BeforeTestArrayShift)
227 {
228 $ReturnedObject = array_shift($ReturnedObject);
229 }
230
231 $array_for_test_object = array($TestObject, $TestFunctionName);
232 $array_for_returned_object = array($ReturnedObject, $TestFunctionName);
233
234 $this->assertEquals(call_user_func(
235 $array_for_returned_object, $TestFunctionArguments),
236 call_user_func($array_for_test_object, $TestFunctionArguments));
237
238 $this->assertInstanceOf($TestObjectType, $ReturnedObject);
239 }
240
241 # clear the value from the field
242 $TestResource->Clear($Field);
243
244 switch ($Field->Type())
245 {
246 case MetadataSchema::MDFTYPE_TEXT:
247 case MetadataSchema::MDFTYPE_URL:
248 case MetadataSchema::MDFTYPE_PARAGRAPH:
249 case MetadataSchema::MDFTYPE_DATE:
250 case MetadataSchema::MDFTYPE_TIMESTAMP:
251 case MetadataSchema::MDFTYPE_NUMBER:
252 case MetadataSchema::MDFTYPE_FLAG:
253 $TgtVal = NULL;
254 break;
255
256 case MetadataSchema::MDFTYPE_TREE:
257 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
258 case MetadataSchema::MDFTYPE_OPTION:
259 case MetadataSchema::MDFTYPE_USER:
260 case MetadataSchema::MDFTYPE_REFERENCE:
261 $TgtVal = array();
262 break;
263
264 case MetadataSchema::MDFTYPE_POINT:
265 $TgtVal = array(
266 "X" => NULL,
267 "Y" => NULL );
268 break;
269
270 default:
271 throw new Exception("Data type not handled.");
272 break;
273
274 }
275
276 $this->assertEquals($TgtVal, $TestResource->Get($Field));
277
278 $RCopy = new Resource($TestResource->Id());
279 $this->assertEquals($TgtVal, $RCopy->Get($Field),
280 "Check that value returned by Get() matches for field type w/ new resource"
281 .$FieldTypeName);
282 }
283
284 # Clean up function-specific objects.
285 $TestResource->Delete();
286 $TestReferenceResource->Delete();
287 $TestClassification->Delete();
288 $TestControlledName->Delete();
289 $TestOptionCName->Delete();
290 }
291 }
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
ControlledName\Create
static Create($Term, $FieldId)
Create a new empty ControlledName if it&#39;s not already present.
Definition: ControlledName.php:29
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
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
Resource\Get
Get($Field, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using field name or field object.
Definition: Resource.php:415
PHP
PHP
Definition: RSSClient--Test.php:12
MetadataSchema\SCHEMAID_DEFAULT
const SCHEMAID_DEFAULT
Definition: MetadataSchema.php:53
StdLib\GetConstantName
static GetConstantName($ClassName, $Value, $Prefix=NULL)
Get name (string) for constant.
Definition: StdLib.php:853
Resource_Test\$TestFields
static $TestFields
Definition: Resource--Test.php:6
MetadataField
Object representing a locally-defined type of metadata field.
Definition: MetadataField.php:13
MetadataSchema\MDFTYPE_PARAGRAPH
const MDFTYPE_PARAGRAPH
Definition: MetadataSchema.php:22
MetadataSchema\MDFTYPE_DATE
const MDFTYPE_DATE
Definition: MetadataSchema.php:24
Resource
Represents a "resource" in CWIS.
Definition: Resource.php:13
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:48
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\testGetSetClear
testGetSetClear()
This function exercises the Resource get and set methods for each Metadata types using the fields cre...
Definition: Resource--Test.php:85
Resource\Delete
Delete()
Remove resource (and accompanying associations) from database and delete any associated files...
Definition: Resource.php:142