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_CONTROLLEDNAME,
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 testControlledName()
66  {
67  $MyId = self::$TestFieldIds['ControlledNameTestField'];
68 
69  # create a new name
70  $TestName = ControlledName::Create("TestName", $MyId);
71  $this->assertInstanceOf(
72  ControlledName::class, $TestName);
73  $this->assertEquals(
74  $TestName->FieldId(), $MyId);
75  $this->assertEquals(
76  $TestName->Name(), "TestName");
77  $this->assertEquals(
78  $TestName->InUse(), 0);
79  $this->assertEquals(
80  $TestName->GetAssociatedResources(), array() );
81  $this->assertEquals(
82  $TestName->VariantName(), NULL);
83  $this->assertEquals(
84  $TestName->Qualifier(), NULL);
85 
86  # test setting / updating / clearing variants
87  $this->assertEquals(
88  $TestName->VariantName("TestVariant"), "TestVariant");
89  $this->assertEquals(
90  $TestName->VariantName(), "TestVariant");
91  $this->assertEquals(
92  $TestName->VariantName("ChangedVariant"), "ChangedVariant");
93  $this->assertEquals(
94  $TestName->VariantName(), "ChangedVariant");
95  $this->assertEquals(
96  $TestName->VariantName(FALSE), NULL);
97  $this->assertEquals(
98  $TestName->VariantName(), NULL);
99 
100  # test setting / clearing Qualifiers
101  $MyQual = Qualifier::Create("TestQual");
102  $this->assertEquals(
103  $TestName->Qualifier($MyQual)->Id(), $MyQual->Id());
104  $this->assertEquals(
105  $TestName->Qualifier()->Id(), $MyQual->Id());
106 
107  $this->assertEquals(
108  $TestName->QualifierId(NULL), NULL);
109  $this->assertEquals(
110  $TestName->Qualifier(), NULL);
111 
112  $MyQual->Destroy();
113 
114  # look up CNID
115  $this->assertEquals(
116  array($TestName->Id()),
118  "TestName", $MyId));
119 
120  # Create a duplicate of the name
121  $this->assertEquals(ControlledName::ControlledNameExists(
122  "TestName", $MyId), TRUE);
123  $TestDup = ControlledName::Create("TestName", $MyId);
124  $this->assertEquals(
125  $TestDup->Id(), $TestName->Id() );
126 
127  # load an invalid name
128  try
129  {
130  $ExpIsThrown = FALSE;
131  $TestInv = new ControlledName(-5000);
132  }
133  catch (Exception $E)
134  {
135  $ExpIsThrown = TRUE;
136  $this->assertEquals(get_class($E), "InvalidArgumentException");
137  }
138  $this->assertEquals($ExpIsThrown, TRUE);
139 
140  # delete a name
141  $TestName->Delete(TRUE);
142  }
143 }
static ControlledNameExists($Term, $FieldId)
Check if there exists a controlledname with a ControlledName and FieldId same as given.
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.
static Create($Term, $FieldId)
Create a new empty ControlledName if it&#39;s not already present.
SQL database abstraction object with smart query caching.
Definition: Database.php:22
Metadata type representing non-hierarchical controlled vocabulary values.
const MDFTYPE_CONTROLLEDNAME
static Create()
Initialize a new qualifier.
Definition: Qualifier.php:19
static tearDownAfterClass()
After to running the tests, this function is run.
static setUpBeforeClass()
Prior to running any of the tests, this function is run.