CWIS Developer Documentation
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();
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->AddField($FieldName, $FieldType);
40  $TmpField->IsTempItem(FALSE);
41  self::$TestFieldIds[$FieldName] = $TmpField->Id();
42  }
43 
44  # Resource::Create() expects a user to be logged in,
45  # so log in the admin
46  $AdminUser = new CWUser(1);
47 
48  $GLOBALS["G_User"]->Login($AdminUser->Name(), "", TRUE);
49  }
50 
55  public static function tearDownAfterClass()
56  {
57  # construct the schema object
58  $Schema = new MetadataSchema();
59  $Database = new Database();
60 
61  # drop all of the test fields
62  foreach (self::$TestFieldIds as $FieldName => $FieldId)
63  {
64  $Schema->DropField($FieldId);
65 
66  # remove from OAIFieldMappings too
67  $Database->Query("
68  DELETE FROM OAIFieldMappings
69  WHERE SPTFieldId = " . addslashes($FieldId));
70  }
71  }
72 
77  public function testGetandSet()
78  {
79  # Create test-specific objects
81  $TestResource->IsTempResource(FALSE);
82  $TestReferenceResource = Resource::Create(MetadataSchema::SCHEMAID_DEFAULT);
83  $TestReferenceResource->IsTempResource(FALSE);
84  $TestClassification = new Classification(NULL, "TestClassification",
85  self::$TestFieldIds['TestTreeField']);
86  $TestControlledName = new ControlledName(NULL, "TestControlledName",
87  self::$TestFieldIds['TestCNameField']);
88  $TestOptionCName = new ControlledName(NULL, "TestOptionCName",
89  self::$TestFieldIds['TestOptionField']);
90 
91  # Test get and set for each field
92  foreach (self::$TestFields as $FieldName => $FieldType)
93  {
94  # whether, before testing equivalence, we need to pop the
95  # returned value out of an array
96  $BeforeTestArrayShift = FALSE;
97 
98  # if we're testing the object return, this is the object we'll compare it to.
99  unset($TestObject);
100 
101  switch ($FieldType)
102  {
104  $TgtVal = "A test title";
105  break;
106 
108  $TgtVal = "http://testtesttest.com";
109  break;
110 
112  $TgtVal = "I am a test paragraph.";
113  break;
114 
116  $TgtVal = "0";
117  break;
118 
120  $TgtVal = "1";
121  break;
122 
124  $TgtVal = date("Y-m-d");
125  $TestObject = new Date(strval($TgtVal));
126  $TestObjectType = 'Date';
127  $TestFunctionName = 'BeginDate';
128  $TestFunctionArguments = NULL;
129  break;
130 
132  $TgtVal = date("Y-m-d H:i:s", strtotime($TgtVal));
133  break;
134 
136  $TgtVal = array();
137  $TgtVal[$TestClassification->Id()] = "TestClassification";
138  $TestObject = $TestClassification;
139  $TestObjectType = 'Classification';
140  $TestFunctionName = 'FullName';
141  $TestFunctionArguments = NULL;
142  $BeforeTestArrayShift = TRUE;
143  break;
144 
146  $TgtVal = array();
147  $TgtVal[$TestControlledName->Id()] = "TestControlledName";
148  $TestObject = $TestControlledName;
149  $TestObjectType = 'ControlledName';
150  $TestFunctionName = 'Name';
151  $TestFunctionArguments = NULL;
152  $BeforeTestArrayShift = TRUE;
153  break;
154 
156  $TgtVal = array();
157  $TgtVal[$TestOptionCName->Id()] = "TestOptionCName";
158  $TestObject = $TestOptionCName;
159  $TestObjectType = 'ControlledName';
160  $TestFunctionName = 'Name';
161  $TestFunctionArguments = NULL;
162  $BeforeTestArrayShift = TRUE;
163  break;
164 
166  $TestObject = new CWUser(1);
167  $TgtVal = array( 1 => $TestObject->Name() );
168  $TestObjectType = 'CWUser';
169  $TestFunctionName = 'Id';
170  $TestFunctionArguments = NULL;
171  $BeforeTestArrayShift = TRUE;
172  break;
173 
175  $TgtVal = array();
176  $TgtVal['X'] = 5;
177  $TgtVal['Y'] = 7;
178  break;
179 
181  $TestObject = $TestReferenceResource;
182  $TgtVal = array();
183  $TgtVal[$TestReferenceResource->Id()] = $TestReferenceResource->Id();
184  $TestFunctionName = 'Id';
185  $TestObjectType = 'Resource';
186  $TestFunctionArguments = NULL;
187  $BeforeTestArrayShift = TRUE;
188  break;
189 
190  default:
191  throw new Exception("Data type not handled.");
192  break;
193 
194  }
195 
196  # set the value on the test resource
197  $TestResource->Set($FieldName, $TgtVal);
198 
199  # assert the default get returns the expected value
200  $this->assertEquals($TgtVal, $TestResource->Get($FieldName));
201 
202  if (isset($TestObject))
203  {
204  $ReturnedObject = $TestResource->Get($FieldName, TRUE);
205 
206  if ($BeforeTestArrayShift)
207  {
208  $ReturnedObject = array_shift($ReturnedObject);
209  }
210 
211  $array_for_test_object = array($TestObject, $TestFunctionName);
212  $array_for_returned_object = array($ReturnedObject, $TestFunctionName);
213 
214  $this->assertEquals(call_user_func($array_for_returned_object, $TestFunctionArguments),
215  call_user_func($array_for_test_object, $TestFunctionArguments));
216 
217  $this->assertInstanceOf($TestObjectType, $ReturnedObject);
218  }
219 
220  }
221 
222  # Clean up function-specific objects.
223  $TestResource->Delete();
224  $TestReferenceResource->Delete();
225  $TestClassification->Delete();
226  $TestControlledName->Delete();
227  $TestOptionCName->Delete();
228  }
229 }
static tearDownAfterClass()
After to running the tests, this function is run.
Metadata schema (in effect a Factory class for MetadataField).
SQL database abstraction object with smart query caching.
Definition: Database.php:22
Definition: Date.php:18
Metadata type representing non-hierarchical controlled vocabulary values.
const MDFTYPE_CONTROLLEDNAME
static setUpBeforeClass()
Prior to running any of the tests, this function is run.
static $TestFields
static $TestFieldIds
static Create($SchemaId)
Create a new resource.
Definition: Resource.php:66
Metadata type representing hierarchical ("Tree") controlled vocabulary values.
CWIS-specific user class.
Definition: CWUser.php:13
testGetandSet()
This function exercises the Resource get and set methods for each Metadata types using the fields cre...