13 # construct the schema object 16 self::$TestFieldIds = array();
18 # outline fields to be created 19 self::$TestFields = array(
36 foreach (self::$TestFields as $FieldName => $FieldType)
38 $TmpField = $Schema->GetItemByName($FieldName);
39 if ($TmpField === NULL)
41 $TmpField = $Schema->AddField($FieldName, $FieldType);
43 $TmpField->IsTempItem(FALSE);
44 self::$TestFieldIds[$FieldName] = $TmpField->Id();
47 # Resource::Create() expects a user to be logged in, 48 # so log in an admin user 50 $Users = $UFactory->GetUsersWithPrivileges(
51 PRIV_RESOURCEADMIN, PRIV_COLLECTIONADMIN);
52 $UserIds = array_keys($Users);
53 $AdminUserId = array_pop($UserIds);
54 self::$AdminUser =
new CWUser($AdminUserId);
55 $GLOBALS[
"G_User"]->Login(self::$AdminUser->Name(),
"", TRUE);
57 # Create Classification, ControlledName, and Option values 59 "Test Classification", self::$TestFieldIds[
'Test Tree Field']);
61 "Test Controlled Name", self::$TestFieldIds[
'Test CName Field']);
63 "Test Option Name", self::$TestFieldIds[
'Test Option Field']);
72 # construct the schema object 76 # drop all of the test fields 77 foreach (self::$TestFieldIds as $FieldName => $FieldId)
79 $Schema->DropField($FieldId);
82 self::$TestClassification->Delete();
83 self::$TestControlledName->Delete();
84 self::$TestOptionCName->Delete();
93 # create test-specific objects 95 $this->assertTrue($TestResource->IsTempResource(),
96 "Check that newly created resources are temporary.");
98 $this->assertFalse($TestResource->IsTempResource(FALSE),
99 "Check resources can be set permanent.");
101 # test Comments features 102 $this->CheckComments($TestResource);
104 # test Ratings features 105 $this->CheckRatings($TestResource);
107 # test permissions-related functions 108 $this->CheckPermissions($TestResource);
110 # test get, set, and clear 112 $TestReferenceResource->IsTempResource(FALSE);
113 $this->CheckGetSetClear($TestResource, $TestReferenceResource);
115 # check that resource schemas can be retrieved 116 $this->CheckGetSchemaForResource(
117 $TestResource, $TestReferenceResource);
119 # check that GetAsArray works 120 $this->CheckGetAsArray(
121 $TestResource, $TestReferenceResource);
123 # check that perm resource can be made temporary and don't 124 # lose any values in the process 125 $this->CheckTempToggle($TestResource);
128 # clean up function-specific objects 129 $TestResource->Delete();
130 $TestReferenceResource->Delete();
138 private function CheckGetSetClear($Resource, $RefResource)
140 # test get, set, and clear for each test field 141 foreach (self::$TestFieldIds as $FieldName => $FieldId)
145 # whether, before testing equivalence, we need to pop the 146 # returned value out of an array 147 $BeforeTestArrayShift = FALSE;
149 # if we're testing the object return, this is the object we'll compare it to. 152 switch ($Field->Type())
155 $TgtVal =
"A test title";
159 $TgtVal =
"http://testtesttest.com";
163 $TgtVal =
"I am a test paragraph.";
175 $TgtVal = date(
"Y-m-d");
176 $TestObject =
new Date(strval($TgtVal));
177 $TestObjectType =
'Date';
178 $TestFunctionName =
'BeginDate';
179 $TestFunctionArguments = NULL;
183 $TgtVal = date(
"Y-m-d H:i:s");
188 $TgtVal[self::$TestClassification->Id()] =
"Test Classification";
189 $TestObject = self::$TestClassification;
190 $TestObjectType =
'Classification';
191 $TestFunctionName =
'FullName';
192 $TestFunctionArguments = NULL;
193 $BeforeTestArrayShift = TRUE;
198 $TgtVal[self::$TestControlledName->Id()] =
"Test Controlled Name";
199 $TestObject = self::$TestControlledName;
200 $TestObjectType =
'ControlledName';
201 $TestFunctionName =
'Name';
202 $TestFunctionArguments = NULL;
203 $BeforeTestArrayShift = TRUE;
208 $TgtVal[self::$TestOptionCName->Id()] =
"Test Option Name";
209 $TestObject = self::$TestOptionCName;
210 $TestObjectType =
'ControlledName';
211 $TestFunctionName =
'Name';
212 $TestFunctionArguments = NULL;
213 $BeforeTestArrayShift = TRUE;
217 $UserId = $GLOBALS[
"G_User"]->Id();
218 $TestObject =
new CWUser($UserId);
219 $TgtVal = array( $UserId => $TestObject->Name() );
220 $TestObjectType =
'CWUser';
221 $TestFunctionName =
'Id';
222 $TestFunctionArguments = NULL;
223 $BeforeTestArrayShift = TRUE;
233 $TestObject = $RefResource;
235 $TgtVal[$RefResource->Id()] = $RefResource->Id();
236 $TestFunctionName =
'Id';
237 $TestObjectType =
'Resource';
238 $TestFunctionArguments = NULL;
239 $BeforeTestArrayShift = TRUE;
243 throw new Exception(
"Data type not handled.");
248 # set the value on the test resource 249 $Resource->Set($Field, $TgtVal);
251 # assert the default get returns the expected value 253 "MetadataSchema", $Field->Type(),
"MDFTYPE_");
254 $this->assertEquals($TgtVal, $Resource->Get($Field),
255 "Check that value returned by Get() matches for field type " 258 $this->assertTrue($Resource->FieldIsSet($Field),
259 "Check that FieldIsSet() returns TRUE after setting value for field type " 262 $RCopy =
new Resource($Resource->Id());
263 $this->assertEquals($TgtVal, $RCopy->Get($Field),
264 "Check that value returned by Get() matches for field type w/ new resource" 267 if (isset($TestObject))
269 $ReturnedObject = $Resource->
Get($Field, TRUE);
271 if ($BeforeTestArrayShift)
273 $ReturnedObject = array_shift($ReturnedObject);
276 $array_for_test_object = array($TestObject, $TestFunctionName);
277 $array_for_returned_object = array($ReturnedObject, $TestFunctionName);
279 if ($TestFunctionArguments !== NULL)
281 $this->assertEquals(call_user_func(
282 $array_for_returned_object, $TestFunctionArguments),
283 call_user_func($array_for_test_object, $TestFunctionArguments));
287 $this->assertEquals(call_user_func($array_for_returned_object),
288 call_user_func($array_for_test_object));
291 $this->assertInstanceOf($TestObjectType, $ReturnedObject);
294 # clear the value from the field 295 $Resource->Clear($Field);
297 switch ($Field->Type())
324 throw new Exception(
"Data type not handled.");
329 $this->assertEquals($TgtVal, $Resource->Get($Field));
331 $this->assertFalse($Resource->FieldIsSet($Field),
332 "Check that FieldIsSet() returns FALSE after clearing value for field type " 335 $RCopy =
new Resource($Resource->Id());
336 $this->assertEquals($TgtVal, $RCopy->Get($Field),
337 "Check that value returned by Get() matches for field type w/ new resource" 347 private function CheckComments($Resource)
349 $this->assertEquals(0, $Resource->NumberOfComments(),
350 "Check that newly created resources have no comments.");
351 $this->assertNull($Resource->Comments(),
352 "Check that newly created resources have null comment list.");
356 $TestComment->ParentId($Resource->Id());
358 # reload resource to nuke internal caches 359 $Resource =
new Resource($Resource->Id());
361 $this->assertEquals(1, $Resource->NumberOfComments(),
362 "Check that NumberOfComments() is one after adding a single comment.");
366 $this->assertTrue(is_array($RComments),
367 "Check that Comments() returns an array.");
369 $this->assertEquals(1, count($RComments),
370 "Check that Comments() returns an array of length 1");
372 $RComment = array_Shift($RComments);
374 $this->assertTrue($RComment instanceof
Message,
375 "Check that the comment is a Message.");
377 $this->assertEquals($TestComment->Id(), $RComment->Id(),
378 "Check that the CommentId of the single Message in the array returned " 379 .
"by Comments() matches the Id of the test comment " 380 .
"that we just associated with the resource.");
382 $TestComment->Destroy();
384 # reload resource to nuke internal caches 385 $Resource =
new Resource($Resource->Id());
387 $this->assertEquals(0, $Resource->NumberOfComments(),
388 "Check that resource has no comments after deleting comment.");
389 $this->assertNull($Resource->Comments(),
390 "Check that resource has null comment list after deleting comment.");
398 private function CheckRatings($Resource)
400 $this->assertEquals(0, $Resource->NumberOfRatings(),
401 "Check that newly created resources have no ratings.");
402 $this->assertEquals(0, $Resource->CumulativeRating(),
403 "Check that newly created resources have no cumulative rating.");
404 $this->assertEquals(0, $Resource->ScaledCumulativeRating(),
405 "Check that newly created resources have no scaled cumulative rating.");
408 $this->assertNull($Resource->Rating(),
409 "Check that admin user hasn't rated this resource.");
410 $this->assertEquals(25, $Resource->Rating(25),
411 "Check that admin user can rate this resource.");
412 $this->assertEquals(25, $Resource->Rating(),
413 "Check that admin's rating was saved");
414 $this->assertEquals(1, $Resource->NumberOfRatings(),
415 "Check that number of ratings is correct.");
416 $this->assertEquals(25, $Resource->CumulativeRating(),
417 "Check that cumulative rating is correct.");
418 $this->assertEquals(3, $Resource->ScaledCumulativeRating(),
419 "Check that scaled cumulative rating is correct.");
420 $this->assertEquals(50, $Resource->Rating(50),
421 "Check that admin can change rating.");
422 $this->assertEquals(1, $Resource->NumberOfRatings(),
423 "Check that number of ratings is correct.");
424 $this->assertEquals(50, $Resource->CumulativeRating(),
425 "Check that cumulative rating is correct.");
426 $this->assertEquals(5, $Resource->ScaledCumulativeRating(),
427 "Check that scaled cumulative rating is correct.");
429 $GLOBALS[
"G_User"]->Logout();
432 "Check that anon user hasn't rated this resource.");
433 $GLOBALS[
"G_User"]->Login(self::$AdminUser->Name(),
"", TRUE);
446 private function CheckPermissions($Resource)
448 $TitleField = $Resource->Schema()->GetFieldByMappedName(
"Title");
449 foreach ([
"View",
"Edit",
"Author",
"Modify"] as $Action)
451 $CheckFn =
"UserCan".$Action;
452 $FieldCheckFn =
"UserCan".$Action.
"Field";
456 "Check that Anon users cannot ".strtolower($Action)
457 .
" a new Resource.");
460 "Check that Anon users cannot ".strtolower($Action)
461 .
" the Title field on a new Resource.");
464 $Resource->UserCanView(self::$AdminUser),
465 "Check that admin users can ".strtolower($Action)
466 .
" a new Resource.");
468 $Resource->$FieldCheckFn(self::$AdminUser, $TitleField),
469 "Check that admin users can ".strtolower($Action)
470 .
" the Title field on a new Resource.");
475 "Check that Anon users cannot view mapped Title on a new Resource.");
478 $Resource->UserCanViewField(self::$AdminUser, PHP_INT_MAX),
479 "Check that users cannot view invalid fields.");
481 $Field = $Resource->Schema()->GetField(
"Test Text Field");
482 $Field->Enabled(FALSE);
485 # do disabled field check on a copy of the resource so that 486 # the PermissionCache doesn't cause it to succeed erroneously 487 $RCopy =
new Resource($Resource->Id());
489 $RCopy->UserCanViewField(self::$AdminUser, $Field),
490 "Check that users cannot view disabled fields.");
491 $Field->Enabled(TRUE);
499 private function CheckGetSchemaForResource($Resource, $RefResource)
504 "Check that GetSchemaIdForResource() is correct with a single resource.");
510 TRUE,
"GetSchemaForResource() did not throw exception on invalid Id.");
515 $e instanceof InvalidArgumentException,
516 "GetSchemaForResource() threw wrong exception type.");
519 $Ids = [$Resource->Id(), $RefResource->Id()];
523 "Check that GetSchemaIdForResource() is correct with multiple resources.");
530 TRUE,
"GetSchemaForResource() did not throw exception on invalid Id.");
535 $e instanceof InvalidArgumentException,
536 "GetSchemaForResource() threw wrong exception type.");
545 private function CheckGetAsArray($Resource, $RefResource)
548 "Test Text Field" =>
"TestValue",
549 "Test Url Field" =>
"http://example.com",
550 "Test Reference Field" =>
551 [$RefResource->Id() => $RefResource->Id()],
553 [$GLOBALS[
"G_User"]->Id() => $GLOBALS[
"G_User"]->Get(
"UserName")],
554 "Test Option Field" =>
555 [self::$TestOptionCName->Id() => self::$TestOptionCName->Name()],
556 "Test CName Field" =>
557 [self::$TestControlledName->Id() => self::$TestControlledName->Name()],
559 [self::$TestClassification->Id() => self::$TestClassification->FullName()],
562 foreach ($Values as $FieldName => $Value)
564 $Resource->Set($FieldName, $Value);
567 $Result = $Resource->GetAsArray(FALSE, FALSE);
569 # subset to just the fields that we've set 570 $Result = array_intersect_key($Result, $Values);
573 $Values, $Result,
"Checking GetAsArray()");
581 private function CheckTempToggle($Resource)
584 $Resource->IsTempResource(),
585 "Check that provided resource is permanent.");
587 $Before = $Resource->GetAsArray(TRUE, FALSE);
590 $Resource->IsTempResource(TRUE),
591 "Check that permanent resources can be made temporary.");
593 $After = $Resource->GetAsArray(TRUE, FALSE);
594 unset($Before[
"ResourceId"]);
595 unset($After[
"ResourceId"]);
599 "Check that resource values don't change on perm/temp toggle");
601 $RCopy =
new Resource($Resource->Id());
604 unset($AfterCopy[
"ResourceId"]);
608 "Check that resource values don't change on perm/temp toggle " 609 .
"w/ newly loaded resource");
static GetAnonymousUser()
Get the anonymous user (i.e., the User object that exists when no user is logged in), useful when a permission check needs to know if something should be visible to the general public.
static tearDownAfterClass()
After to running the tests, this function is run.
Abstraction for forum messages and resource comments.
static $TestControlledName
static Create($Term, $FieldId)
Create a new empty ControlledName if it's not already present.
static Create()
Create an empty message object.
SQL database abstraction object with smart query caching.
static Create($Name, $FieldId, $ParentId=NULL)
Add new classification to the hierarchy.
GetAsArray($IncludeDisabledFields=FALSE, $ReturnObjects=TRUE)
Retrieve all resource values as an array.
const PARENTTYPE_RESOURCE
Comments()
Get comments for resource.
static setUpBeforeClass()
Prior to running any of the tests, this function is run.
CWIS-specific user factory class.
Get($Field, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using field name or field object.
static GetSchemaForResource($ResourceId)
Get schema ID for specified resource(s).
static GetConstantName($ClassName, $Value, $Prefix=NULL)
Get name (string) for constant.
testResource()
This function exercises the Resource get and set methods for each Metadata types using the fields cre...
Represents a "resource" in CWIS.
static $TestClassification
static Create($SchemaId)
Create a new resource.
CWIS-specific user class.