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();
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
80 $TestResource = Resource::Create(MetadataSchema::SCHEMAID_DEFAULT);
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 {
103 case MetadataSchema::MDFTYPE_TEXT:
104 $TgtVal = "A test title";
105 break;
106
107 case MetadataSchema::MDFTYPE_URL:
108 $TgtVal = "http://testtesttest.com";
109 break;
110
111 case MetadataSchema::MDFTYPE_PARAGRAPH:
112 $TgtVal = "I am a test paragraph.";
113 break;
114
115 case MetadataSchema::MDFTYPE_NUMBER:
116 $TgtVal = "0";
117 break;
118
119 case MetadataSchema::MDFTYPE_FLAG:
120 $TgtVal = "1";
121 break;
122
123 case MetadataSchema::MDFTYPE_DATE:
124 $TgtVal = date("Y-m-d");
125 $TestObject = new Date(strval($TgtVal));
126 $TestObjectType = 'Date';
127 $TestFunctionName = 'BeginDate';
128 $TestFunctionArguments = NULL;
129 break;
130
131 case MetadataSchema::MDFTYPE_TIMESTAMP:
132 $TgtVal = date("Y-m-d H:i:s", strtotime($TgtVal));
133 break;
134
135 case MetadataSchema::MDFTYPE_TREE:
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
145 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
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
155 case MetadataSchema::MDFTYPE_OPTION:
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
165 case MetadataSchema::MDFTYPE_USER:
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
174 case MetadataSchema::MDFTYPE_POINT:
175 $TgtVal = array();
176 $TgtVal['X'] = 5;
177 $TgtVal['Y'] = 7;
178 break;
179
180 case MetadataSchema::MDFTYPE_REFERENCE:
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 }
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:55
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
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
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:66
Classification
Metadata type representing hierarchical ("Tree") controlled vocabulary values.
Definition: Classification.php:13
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:77