CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

Classification--Test.php
Go to the documentation of this file.
1 <?PHP
2
3 class Classification_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 MetadataSchema::SCHEMAID_DEFAULT);
19
20 self::$TestFieldIds = array();
21
22 # outline fields to be created
23 self::$TestFields = array(
24 "ClassificationTestField" => MetadataSchema::MDFTYPE_TREE,
25 );
26
27 # create the fields
28 foreach (self::$TestFields as $FieldName => $FieldType)
29 {
30 $TmpField = $Schema->GetItemByName($FieldName);
31 if ($TmpField === NULL)
32 {
33 $TmpField = $Schema->AddField($FieldName, $FieldType);
34 }
35 $TmpField->IsTempItem(FALSE);
36 self::$TestFieldIds[$FieldName] = $TmpField->Id();
37 }
38
39 }
40
45 public static function tearDownAfterClass()
46 {
47 # construct the schema object
48 $Schema = new MetadataSchema(
49 MetadataSchema::SCHEMAID_DEFAULT);
50 $Database = new Database();
51
52 # drop all of the test fields
53 foreach (self::$TestFieldIds as $FieldName => $FieldId)
54 {
55 $Schema->DropField($FieldId);
56
57 # remove from OAIFieldMappings too
58 $Database->Query("
59 DELETE FROM OAIFieldMappings
60 WHERE SPTFieldId = " . addslashes($FieldId));
61 }
62 }
63
64
65 public function testClassification()
66 {
67 $MyId = self::$TestFieldIds['ClassificationTestField'];
68
69 $TestParent = Classification::Create(
70 "TestParent", $MyId);
71
72 $this->assertInstanceOf(
73 Classification::class, $TestParent);
74 $this->assertEquals(
75 $TestParent->FieldId(), $MyId);
76
77
78 # attempt to create a duplicate w/o setting a parentid
79 try
80 {
81 Classification::Create(
82 "TestParent", $MyId);
83 $this->fail(
84 "Exception not thrown on creation of duplicate Classification");
85 }
86 catch (Exception $e)
87 {
88 $this->assertEquals(
89 $e->getMessage(),
90 "Duplicate name specified for new classification "
91 ."(TestParent).");
92 }
93
94 # attempt to create a duplicate top-level class
95 try
96 {
97 Classification::Create(
98 "TestParent", $MyId, Classification::NOPARENT);
99 $this->fail(
100 "Exception not thrown on creation of duplicate Classification");
101 }
102 catch (Exception $e)
103 {
104 $this->assertEquals(
105 $e->getMessage(),
106 "Duplicate name specified for new classification "
107 ."(TestParent).");
108 }
109
110 # attempt to create a child with an invalid parent
111 try
112 {
113 Classification::Create(
114 "TestBadChild", $MyId, -10);
115 $this->fail(
116 "Exception not thrown when creating with invalid ParentId");
117 }
118 catch (Exception $e)
119 {
120 $this->assertInstanceOf(
121 InvalidArgumentException::class, $e);
122 $this->assertEquals(
123 $e->getMessage(),
124 "Invalid parent ID specified (-10).");
125 }
126
127 # attempt to create a child, specifying the parent
128 $TestFirstChild = Classification::Create(
129 "FirstChild", $MyId, $TestParent->Id() );
130 $this->assertInstanceOf(
131 Classification::class, $TestFirstChild);
132 $this->assertEquals(
133 $TestFirstChild->ParentId(),
134 $TestParent->Id() );
135
136 # attempt to create a child, not specifying the parent
137 $TestSecondChild = Classification::Create(
138 "TestParent -- SecondChild", $MyId);
139 $this->assertInstanceOf(
140 Classification::class, $TestSecondChild);
141 $this->assertEquals(
142 Classification::SegmentsCreated(), 1);
143
144 # test the various ways to retrieve our name
145 $this->assertEquals(
146 $TestSecondChild->Name(),
147 "TestParent -- SecondChild");
148
149 $this->assertEquals(
150 $TestSecondChild->FullName(),
151 "TestParent -- SecondChild");
152
153 $this->assertEquals(
154 $TestSecondChild->SegmentName(),
155 "SecondChild");
156
157 $this->assertNull(
158 $TestSecondChild->VariantName());
159
160 # test recalculating
161 $IdsUpdated = $TestSecondChild->RecalcResourceCount();
162
163 $Expected = array(
164 $TestSecondChild->Id(),
165 $TestParent->Id() );
166
167 sort($IdsUpdated);
168 sort($Expected);
169
170 $this->assertEquals($IdsUpdated, $Expected);
171
172 # Test initial Depth values
173 $this->assertEquals(
174 $TestSecondChild->Depth(), 1);
175 $this->assertEquals(
176 $TestParent->Depth(), 0);
177
178
179 # test recalculating depth
180 $TestParent->RecalcDepthAndFullName();
181
182 # verify that names and depths remain correct after recalc
183 $this->assertEquals(
184 $TestSecondChild->FullName(),
185 "TestParent -- SecondChild");
186 $this->assertEquals(
187 $TestSecondChild->SegmentName(),
188 "SecondChild");
189
190 $this->assertEquals(
191 $TestSecondChild->Depth(), 1);
192 $this->assertEquals(
193 $TestParent->Depth(), 0);
194
195 $this->assertEquals(
196 $TestSecondChild->FullResourceCount(), 0);
197
198
199 # test listing children
200 $ChildIds = $TestParent->ChildList();
201 $Expected = array(
202 $TestFirstChild->Id(),
203 $TestSecondChild->Id() );
204 sort($ChildIds);
205 sort($Expected);
206 $this->assertEquals($ChildIds, $Expected);
207
208 # test deleting
209 $DelCount = $TestSecondChild->Delete();
210 $this->assertEquals(
211 $DelCount, 1);
212
213 # test deletions that eat our parents
214 $DelCount = $TestFirstChild->Delete(TRUE);
215 $this->assertEquals(
216 $DelCount, 2);
217
218 }
219 }
Classification_Test
Definition: Classification--Test.php:3
Classification\SegmentsCreated
static SegmentsCreated()
Get number of new segments (Classifications) generated when creating a new Classification with a full...
Definition: Classification.php:263
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
Classification_Test\$TestFieldIds
static $TestFieldIds
Definition: Classification--Test.php:5
Classification\NOPARENT
const NOPARENT
Parent value for classifications with no parent.
Definition: Classification.php:18
MetadataSchema\MDFTYPE_TREE
const MDFTYPE_TREE
Definition: MetadataSchema.php:27
Classification_Test\tearDownAfterClass
static tearDownAfterClass()
After to running the tests, this function is run.
Definition: Classification--Test.php:45
Classification_Test\testClassification
testClassification()
Definition: Classification--Test.php:65
PHP
PHP
Definition: RSSClient--Test.php:12
Classification_Test\$TestFields
static $TestFields
Definition: Classification--Test.php:6
MetadataSchema\SCHEMAID_DEFAULT
const SCHEMAID_DEFAULT
Definition: MetadataSchema.php:53
Classification_Test\setUpBeforeClass
static setUpBeforeClass()
Prior to running any of the tests, this function is run.
Definition: Classification--Test.php:14