1
+ <?php
2
+
3
+ namespace Tests \Feature ;
4
+
5
+ use App \Classes \LDAP \Attribute ;
6
+ use App \Ldap \Entry ;
7
+ use Tests \TestCase ;
8
+
9
+ /**
10
+ * This unit will test Attributes that are:
11
+ * + no_attr_tag attributes vs those with attr_tags, AND
12
+ * + md5 attributes vs those that are not md5 attributes
13
+ *
14
+ * objectClass (a no_attr_tags_attribute)
15
+ * userPassword (a no_attr_tags_attribute, and an md5 attribute)
16
+ * certificate (a no_attr_tags attribute)
17
+ * [internal attribute] (which is a no_attr_tags attribute)
18
+ * mail (a normal attribute)
19
+ *
20
+ * => no_lang_tag attributes
21
+ * + ->values returns a Collection of values
22
+ * + ->values_old return a Collection of old values
23
+ * + ->tagValues() returns a Collection of values
24
+ * + ->tagValuesOld() return a Collection of old values
25
+ * + ->render_old_item() should be the raw value (unless an md5attribute, then the md5 value)
26
+ * + ->render_new_item() should be the raw value (unless an md5attribute, then the md5 value)
27
+ * + ->_values is array with only 1 key _null_ with an array of values
28
+ * + ->_values_old is array with only 1 key _null_ with an array of values
29
+ * + ->isDirty processing when there is a new value in the _null_ key and in another key (it should be ignored for no_attr_tags attributes)
30
+ * + ->isDirty processing when there is a new value, and its an md5 attribute
31
+ *
32
+ * The goal here is that
33
+ * + no_attr_tags attributes return an array of values not indexed by an attr_tag
34
+ * + attr_tag attributes are an array of values indexed by an attr_tag
35
+ * + md5 attributes will render the md5 value, and compare the md5 value when determining if it has changed
36
+ *
37
+ * This will mean that our views then can render attributes with tagValues() and render_xxx_item() without just by calling
38
+ * those methods with the langtag for the attribute rendering
39
+ *
40
+ * Attributes that are no_attr_tag attributes should not render anything in non-default langtag views
41
+ */
42
+ class AttributeTagsTest extends TestCase
43
+ {
44
+ private function read ()
45
+ {
46
+ static $ o = NULL ;
47
+
48
+ if (is_null ($ o )) {
49
+ $ dn = 'cn=Bart Simpson,ou=People,o=Simpsons ' ;
50
+ $ this ->assertTrue ($ this ->login ());
51
+ $ this ->assertEquals ($ dn ,$ o =config ('server ' )->fetch ($ dn ));
52
+ }
53
+
54
+ return $ o ;
55
+ }
56
+
57
+ public function test_uid ()
58
+ {
59
+ // Test UID, which can have attribute tags
60
+ $ o = $ this ->read ();
61
+ $ new = ['newbart ' ];
62
+ $ o ->uid = [
63
+ '_null_ ' => $ new ,
64
+ ];
65
+
66
+ $ oo = $ o ->getObject ('uid ' );
67
+
68
+ $ this ->assertInstanceOf (Attribute::class,$ oo );
69
+
70
+ // ->values returns a Collection of values
71
+ // ->_values is array with only 1 key _null_ with an array of values
72
+ $ this ->assertCount (1 ,$ oo ->values );
73
+ $ this ->assertArrayHasKey (Entry::TAG_NOTAG ,$ oo ->values );
74
+ $ this ->assertCount (1 ,$ oo ->values [Entry::TAG_NOTAG ]);
75
+
76
+ // ->values_old return a Collection of old values
77
+ // ->_values_old is array with only 1 key _null_ with an array of values
78
+ $ this ->assertCount (1 ,$ oo ->values_old );
79
+ $ this ->assertArrayHasKey (Entry::TAG_NOTAG ,$ oo ->values_old );
80
+ $ this ->assertCount (1 ,$ oo ->values_old [Entry::TAG_NOTAG ]);
81
+
82
+ // ->tagValues() returns a Collection of values
83
+ $ this ->assertCount (1 ,$ oo ->tagValues ());
84
+
85
+ // ->tagValuesOld() return a Collection of old values
86
+ $ this ->assertCount (1 ,$ oo ->tagValuesOld ());
87
+
88
+ // ->render_item_old() should be the raw value (unless an md5attribute, then the md5 value)
89
+ $ this ->assertEquals ('bart ' ,$ oo ->render_item_old (Entry::TAG_NOTAG .'.0 ' ));
90
+ // ->render_item_new() should be the raw value (unless an md5attribute, then the md5 value)
91
+ $ this ->assertEquals ('newbart ' ,$ oo ->render_item_new (Entry::TAG_NOTAG .'.0 ' ));
92
+
93
+ // ->isDirty processing when there is a new value in the _null_ key and in another key (it should be ignored for no_attr_tags attributes)
94
+ // ->isDirty processing when there is a new value, and its an md5 attribute
95
+ $ this ->assertTrue ($ oo ->isDirty ());
96
+ $ this ->assertCount (1 ,$ x =$ o ->getDirty ());
97
+ $ this ->assertArrayHasKey ('uid ' ,$ x );
98
+ $ this ->assertCount (1 ,$ x ['uid ' ]);
99
+ $ this ->assertEquals ($ new ,$ x ['uid ' ]);
100
+ }
101
+
102
+ public function test_objectclass ()
103
+ {
104
+ // Test ObjectClass, which can NOT have attribute tags
105
+ $ o = $ this ->read ();
106
+ $ newoc = [
107
+ 'inetOrgPerson ' ,
108
+ 'posixAccount ' ,
109
+ 'top ' ,
110
+ 'shadowAccount ' ,
111
+ 'inetLocalMailRecipient ' ,
112
+ ];
113
+
114
+ $ o ->objectclass = [
115
+ '_null_ ' => $ newoc ,
116
+ ];
117
+
118
+ $ oo = $ o ->getObject ('objectclass ' );
119
+
120
+ $ this ->assertInstanceOf (Attribute \ObjectClass::class,$ oo );
121
+ $ this ->assertTrue ($ oo ->no_attr_tags );
122
+
123
+ // ->values returns a Collection of values
124
+ // ->_values is array with only 1 key _null_ with an array of values
125
+ $ this ->assertCount (5 ,$ oo ->values );
126
+ $ this ->assertArrayNotHasKey (Entry::TAG_NOTAG ,$ oo ->values );
127
+
128
+ // ->values_old return a Collection of old values
129
+ // ->_values_old is array with only 1 key _null_ with an array of values
130
+ $ this ->assertCount (4 ,$ oo ->values_old );
131
+ $ this ->assertArrayNotHasKey (Entry::TAG_NOTAG ,$ oo ->values_old );
132
+
133
+ // ->tagValues() returns a Collection of values
134
+ $ this ->assertCount (5 ,$ oo ->tagValues ());
135
+
136
+ // ->tagValuesOld() return a Collection of old values
137
+ $ this ->assertCount (4 ,$ oo ->tagValuesOld ());
138
+
139
+ // ->render_item_old() should be the raw value (unless an md5attribute, then the md5 value)
140
+ $ this ->assertEquals ('inetOrgPerson ' ,$ oo ->render_item_old ('0 ' ));
141
+ // ->render_item_new() should be the raw value (unless an md5attribute, then the md5 value)
142
+ $ this ->assertEquals ('inetLocalMailRecipient ' ,$ oo ->render_item_new ('4 ' ));
143
+
144
+ // ->isDirty processing when there is a new value in the _null_ key and in another key (it should be ignored for no_attr_tags attributes)
145
+ // ->isDirty processing when there is a new value, and its an md5 attribute
146
+ $ this ->assertTrue ($ oo ->isDirty ());
147
+ $ this ->assertCount (2 ,$ x =$ o ->getDirty ());
148
+ $ this ->assertArrayHasKey ('objectclass ' ,$ x );
149
+ $ this ->assertCount (5 ,$ x ['objectclass ' ]);
150
+ $ this ->assertEquals ($ newoc ,$ x ['objectclass ' ]);
151
+ }
152
+
153
+ public function test_userpassword ()
154
+ {
155
+ // Test ObjectClass, which can NOT have attribute tags
156
+ $ o = $ this ->read ();
157
+ $ new = [
158
+ 'test1234 ' ,
159
+ ];
160
+ $ o ->userpassword = [
161
+ '_null_ ' => $ new ,
162
+ ];
163
+
164
+ $ oo = $ o ->getObject ('userpassword ' );
165
+
166
+ $ this ->assertInstanceOf (Attribute \Password::class,$ oo );
167
+ $ this ->assertTrue ($ oo ->no_attr_tags );
168
+
169
+ // ->values returns a Collection of values
170
+ // ->_values is array with only 1 key _null_ with an array of values
171
+ $ this ->assertCount (1 ,$ oo ->values );
172
+ $ this ->assertArrayNotHasKey (Entry::TAG_NOTAG ,$ oo ->values );
173
+
174
+ // ->values_old return a Collection of old values
175
+ // ->_values_old is array with only 1 key _null_ with an array of values
176
+ $ this ->assertCount (1 ,$ oo ->values_old );
177
+ $ this ->assertArrayNotHasKey (Entry::TAG_NOTAG ,$ oo ->values_old );
178
+
179
+ // ->tagValues() returns a Collection of values
180
+ $ this ->assertCount (1 ,$ oo ->tagValues ());
181
+
182
+ // ->tagValuesOld() return a Collection of old values
183
+ $ this ->assertCount (1 ,$ oo ->tagValuesOld ());
184
+
185
+ // ->render_item_old() should be the raw value (unless an md5attribute, then the md5 value)
186
+ $ this ->assertEquals ('{*clear*}**************** ' ,$ oo ->render_item_old ('0 ' ));
187
+ // ->render_item_new() should be the raw value (unless an md5attribute, then the md5 value)
188
+ $ this ->assertEquals ('**************** ' ,$ oo ->render_item_new ('0 ' ));
189
+
190
+ // ->isDirty processing when there is a new value in the _null_ key and in another key (it should be ignored for no_attr_tags attributes)
191
+ // ->isDirty processing when there is a new value, and its an md5 attribute
192
+ $ this ->assertTrue ($ oo ->isDirty ());
193
+ $ this ->assertCount (3 ,$ x =$ o ->getDirty ());
194
+ $ this ->assertArrayHasKey ('userpassword ' ,$ x );
195
+ $ this ->assertCount (1 ,$ x ['userpassword ' ]);
196
+ $ this ->assertEquals ($ new ,$ x ['userpassword ' ]);
197
+ }
198
+
199
+ public function test_userpassword_nochange ()
200
+ {
201
+ // Test ObjectClass, which can NOT have attribute tags
202
+ $ o = $ this ->read ();
203
+ $ new = [
204
+ 'd88d98df6727f87376c93e9676978146 ' , // eatmyshorts
205
+ ];
206
+ $ o ->userpassword = [
207
+ '_null_ ' => $ new ,
208
+ ];
209
+
210
+ $ oo = $ o ->getObject ('userpassword ' );
211
+
212
+ $ this ->assertInstanceOf (Attribute \Password::class,$ oo );
213
+ $ this ->assertTrue ($ oo ->no_attr_tags );
214
+
215
+ // ->values returns a Collection of values
216
+ // ->_values is array with only 1 key _null_ with an array of values
217
+ $ this ->assertCount (1 ,$ oo ->values );
218
+ $ this ->assertArrayNotHasKey (Entry::TAG_NOTAG ,$ oo ->values );
219
+
220
+ // ->values_old return a Collection of old values
221
+ // ->_values_old is array with only 1 key _null_ with an array of values
222
+ $ this ->assertCount (1 ,$ oo ->values_old );
223
+ $ this ->assertArrayNotHasKey (Entry::TAG_NOTAG ,$ oo ->values_old );
224
+
225
+ // ->tagValues() returns a Collection of values
226
+ $ this ->assertCount (1 ,$ oo ->tagValues ());
227
+
228
+ // ->tagValuesOld() return a Collection of old values
229
+ $ this ->assertCount (1 ,$ oo ->tagValuesOld ());
230
+
231
+ // ->render_item_old() should be the raw value (unless an md5attribute, then the md5 value)
232
+ $ this ->assertEquals ('{*clear*}**************** ' ,$ oo ->render_item_old ('0 ' ));
233
+ // ->render_item_new() should be the raw value (unless an md5attribute, then the md5 value)
234
+ $ this ->assertEquals ('**************** ' ,$ oo ->render_item_new ('0 ' ));
235
+
236
+ // ->isDirty processing when there is a new value in the _null_ key and in another key (it should be ignored for no_attr_tags attributes)
237
+ // ->isDirty processing when there is a new value, and its an md5 attribute
238
+ $ this ->assertFalse ($ oo ->isDirty ());
239
+ $ this ->assertCount (2 ,$ x =$ o ->getDirty ());
240
+ $ this ->assertArrayNotHasKey ('userpassword ' ,$ x );
241
+ }
242
+ }
0 commit comments