CWIS Developer Documentation
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(
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(
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  {
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  {
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  {
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(
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 }
static SegmentsCreated()
Get number of new segments (Classifications) generated when creating a new Classification with a full...
Metadata schema (in effect a Factory class for MetadataField).
SQL database abstraction object with smart query caching.
Definition: Database.php:22
static Create($Name, $FieldId, $ParentId=NULL)
Add new classification to the hierarchy.
const NOPARENT
Parent value for classifications with no parent.
static tearDownAfterClass()
After to running the tests, this function is run.
static setUpBeforeClass()
Prior to running any of the tests, this function is run.