CWIS Developer Documentation
ControlledName--Test.php
Go to the documentation of this file.
1 <?PHP
2 
3 class ControlledName_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  "ControlledNameTestField" => 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['ControlledNameTestField'];
68 
69  # Create a new name
70  $TestName = new ControlledName(
71  NULL, "TestName", $MyId);
72  $this->assertInstanceOf(
73  ControlledName::class, $TestName);
74  $this->assertEquals(
75  $TestName->FieldId(), $MyId);
76  $this->assertEquals(
77  $TestName->Name(), "TestName");
78  $this->assertEquals(
79  $TestName->Status(), ControlledName::STATUS_OK);
80  $this->assertEquals(
81  $TestName->InUse(), 0);
82  $this->assertEquals(
83  $TestName->GetAssociatedResources(), array() );
84 
85  # look up CNID
86  $this->assertEquals(
87  array($TestName->Id()),
89  "TestName", $MyId));
90 
91  # Create a duplicate of the name
92  $TestDup = new ControlledName(
93  NULL, "TestName", $MyId);
94  $this->assertEquals(
95  $TestDup->Status(), ControlledName::STATUS_EXISTS);
96  $this->assertEquals(
97  $TestDup->Id(), $TestName->Id() );
98 
99  # Create an invalid name
100  $TestInv = new ControlledName(-5000);
101  $this->assertEquals(
102  $TestInv->Status(), ControlledName::STATUS_INVALID_ID);
103 
104  # Delete a name
105  $TestName->Delete(TRUE);
106  }
107 }
Metadata schema (in effect a Factory class for MetadataField).
static SearchForControlledName($ControlledName, $FieldId)
Check if the given controlled name already exists for a given field ID.
SQL database abstraction object with smart query caching.
Definition: Database.php:22
Metadata type representing non-hierarchical controlled vocabulary values.
const STATUS_OK
Successful execution.
const STATUS_INVALID_ID
No ControlledName exists with specified ID.
static tearDownAfterClass()
After to running the tests, this function is run.
const STATUS_EXISTS
ControlledName already exists with this term.
static setUpBeforeClass()
Prior to running any of the tests, this function is run.