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  }
58 
59 
60  public function testControlledName()
61  {
62  $MyId = self::$TestFieldIds['ControlledNameTestField'];
63 
64  # create a new name
65  $TestName = ControlledName::Create("TestName", $MyId);
66  $this->assertInstanceOf(
67  ControlledName::class, $TestName);
68  $this->assertEquals(
69  $TestName->FieldId(), $MyId);
70  $this->assertEquals(
71  $TestName->Name(), "TestName");
72  $this->assertEquals(
73  $TestName->InUse(), 0);
74  $this->assertEquals(
75  $TestName->GetAssociatedResources(), array() );
76  $this->assertEquals(
77  $TestName->VariantName(), NULL);
78  $this->assertEquals(
79  $TestName->Qualifier(), NULL);
80 
81  # test setting / updating / clearing variants
82  $this->assertEquals(
83  $TestName->VariantName("TestVariant"), "TestVariant");
84  $this->assertEquals(
85  $TestName->VariantName(), "TestVariant");
86  $this->assertEquals(
87  $TestName->VariantName("ChangedVariant"), "ChangedVariant");
88  $this->assertEquals(
89  $TestName->VariantName(), "ChangedVariant");
90  $this->assertEquals(
91  $TestName->VariantName(FALSE), NULL);
92  $this->assertEquals(
93  $TestName->VariantName(), NULL);
94 
95  # test setting / clearing Qualifiers
96  $MyQual = Qualifier::Create("TestQual");
97  $this->assertEquals(
98  $TestName->Qualifier($MyQual)->Id(), $MyQual->Id());
99  $this->assertEquals(
100  $TestName->Qualifier()->Id(), $MyQual->Id());
101 
102  $this->assertEquals(
103  $TestName->QualifierId(NULL), NULL);
104  $this->assertEquals(
105  $TestName->Qualifier(), NULL);
106 
107  $MyQual->Destroy();
108 
109  # look up CNID
110  $this->assertEquals(
111  array($TestName->Id()),
113  "TestName", $MyId));
114 
115  # Create a duplicate of the name
116  $this->assertEquals(ControlledName::ControlledNameExists(
117  "TestName", $MyId), TRUE);
118  $TestDup = ControlledName::Create("TestName", $MyId);
119  $this->assertEquals(
120  $TestDup->Id(), $TestName->Id() );
121 
122  # load an invalid name
123  try
124  {
125  $ExpIsThrown = FALSE;
126  $TestInv = new ControlledName(-5000);
127  }
128  catch (Exception $E)
129  {
130  $ExpIsThrown = TRUE;
131  $this->assertEquals(get_class($E), "InvalidArgumentException");
132  }
133  $this->assertEquals($ExpIsThrown, TRUE);
134 
135  # delete a name
136  $TestName->Delete(TRUE);
137  }
138 }
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.