1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46:
47: class Relation_Test extends PHPUnit_Framework_TestCase {
48: 49: 50: 51: 52: 53:
54: private $rel1;
55:
56: 57: 58: 59: 60: 61:
62: private $person;
63:
64: 65: 66: 67: 68: 69: 70:
71: protected function setUp() {
72: $this->rel1 = new Relation();
73:
74: $rights = new RightsObject();
75: $rights->rebuildTable();
76:
77: $article = new ArticleObject();
78: $article->rebuildTable();
79:
80: $comment = new ArticleCommentObject();
81: $comment->rebuildTable();
82:
83: $this->person = new PersonObject();
84: $this->person->set('displayName', $_SESSION['currentUser']->getDisplayName());
85: $this->person->set('email', $_SESSION['currentUser']->get('email'));
86: $this->person->set('password', 'password');
87: $this->person->rebuildTable();
88: $this->person->save();
89: }
90:
91: 92: 93: 94: 95: 96: 97:
98: protected function tearDown() {
99: unset($this->rel1);
100: $person = new PersonObject();
101: $person->dropTable();
102:
103: $rights = new RightsObject();
104: $rights->dropTable();
105: $rights->dropTable('Person2Rights');
106:
107: $comment = new ArticleCommentObject();
108: $comment->dropTable();
109:
110: $article = new ArticleObject();
111: $article->dropTable();
112: }
113:
114: 115: 116: 117: 118:
119: public function testSetRelatedClassPass() {
120: try {
121: $this->rel1->setRelatedClass('ArticleObject');
122: }catch (AlphaException $e) {
123: $this->fail('Testing passing a valid BO name to setRelatedClass');
124: }
125: }
126:
127: 128: 129: 130: 131:
132: public function testSetRelatedClassFail() {
133: try {
134: $this->rel1->setRelatedClass('XyzObject');
135: $this->fail('Testing passing an invalid BO name to setRelatedClass');
136: }catch (AlphaException $e) {
137: $this->assertEquals('The class [XyzObject] is not defined anywhere!'
138: , $e->getMessage()
139: , 'Testing passing an invalid BO name to setRelatedClass');
140: }
141: }
142:
143: 144: 145: 146: 147:
148: public function testSetRelatedClassFieldPass() {
149: try {
150: $this->rel1->setRelatedClass('PersonObject');
151: $this->rel1->setRelatedClassField('email');
152: }catch (AlphaException $e) {
153: $this->fail('Testing passing a valid field name to setRelatedClassField');
154: }
155: }
156:
157: 158: 159: 160: 161:
162: public function testSetRelatedClassFieldFail() {
163: try {
164: $this->rel1->setRelatedClass('PersonObject');
165: $this->rel1->setRelatedClassField('doesNotExist');
166: $this->fail('Testing passing an invalid field name to setRelatedClassField');
167: }catch (AlphaException $e) {
168: $this->assertEquals('The field [doesNotExist] was not found in the class [PersonObject]'
169: , $e->getMessage()
170: , 'Testing passing an invalid field name to setRelatedClassField');
171: }
172: }
173:
174: 175: 176: 177: 178:
179: public function testSetRelationTypePass() {
180: try {
181: $this->rel1->setRelationType('MANY-TO-ONE');
182: }catch (AlphaException $e) {
183: $this->fail('Testing passing a valid type name to setRelationType');
184: }
185: }
186:
187: 188: 189: 190: 191:
192: public function testSetRelationTypeFail() {
193: try {
194: $this->rel1->setRelationType('blah');
195: $this->fail('Testing passing an invalid type name to setRelationType');
196: }catch (AlphaException $e) {
197: $this->assertEquals('Relation type of [blah] is invalid!'
198: , $e->getMessage()
199: , 'Testing passing an invalid type name to setRelationType');
200: }
201: }
202:
203: 204: 205: 206: 207:
208: public function testSetValuePass() {
209: try {
210: $this->rel1->setValue(100);
211: $this->rel1->setValue('2777');
212: }catch (AlphaException $e) {
213: $this->fail('Testing setValue method with a valid value');
214: }
215: }
216:
217: 218: 219: 220: 221:
222: public function testSetValueFail() {
223: try {
224: $this->rel1->setValue('xyz');
225: $this->fail('Testing setValue method with an invalid value');
226: }catch (AlphaException $e) {
227: $this->assertEquals('[xyz] not a valid Relation value! A maximum of 11 characters is allowed.'
228: , $e->getMessage()
229: , 'Testing setValue method with an invalid value');
230: }
231: }
232:
233: 234: 235: 236: 237:
238: public function testSetRelatedClassDisplayFieldPass() {
239: try {
240: $this->rel1->setRelatedClass('PersonObject');
241:
242: $this->rel1->setValue(1);
243: $this->rel1->setRelatedClassDisplayField('state');
244: $this->assertEquals('Active', $this->rel1->getRelatedClassDisplayFieldValue(), 'Testing that the display field value of the related class is accessed correctly');
245: }catch (AlphaException $e) {
246: $this->fail('Testing that the display field value of the related class is accessed correctly');
247: }
248: }
249:
250: 251: 252: 253: 254:
255: public function testGetRelatedClassDisplayFieldValueFail() {
256: try {
257: $this->rel1->setRelatedClassDisplayField('someField');
258: $value = $this->rel1->getRelatedClassDisplayFieldValue();
259: $this->fail('Testing that getRelatedClassDisplayFieldValue() will fail to load an invalid class definition');
260: }catch (AlphaException $e) {
261: $this->assertEquals('The class [] is not defined anywhere!'
262: , $e->getMessage()
263: , 'Testing that getRelatedClassDisplayFieldValue() will fail to load an invalid class definition');
264: }
265: }
266:
267: 268: 269: 270: 271:
272: public function testGetRelatedClassDisplayFieldValuePass() {
273: $oneToManyRel = new Relation();
274: $oneToManyRel->setRelatedClass('PersonObject');
275: $oneToManyRel->setRelatedClassField('OID');
276: $oneToManyRel->setRelatedClassDisplayField('displayName');
277: $oneToManyRel->setRelationType('ONE-TO-MANY');
278: $oneToManyRel->setValue($this->person->getOID());
279:
280: $this->assertEquals($_SESSION['currentUser']->getDisplayName(), $oneToManyRel->getRelatedClassDisplayFieldValue(), 'testing the getRelatedClassDisplayFieldValue() method on ONE-TO-MANY relation');
281:
282: $group = new RightsObject();
283: $group->set('name', 'unittestgroup');
284: $group->save();
285:
286: $person1 = new PersonObject();
287: $person1->set('displayName', 'user1');
288: $person1->set('email', 'user1@test.com');
289: $person1->set('password', 'password');
290: $person1->save();
291: $lookup = $person1->getPropObject('rights')->getLookup();
292: $lookup->setValue(array($person1->getOID(), $group->getOID()));
293: $lookup->save();
294:
295: $person2 = new PersonObject();
296: $person2->set('displayName', 'user2');
297: $person2->set('email', 'user2@test.com');
298: $person2->set('password', 'password');
299: $person2->save();
300: $lookup = $person2->getPropObject('rights')->getLookup();
301: $lookup->setValue(array($person2->getOID(), $group->getOID()));
302: $lookup->save();
303:
304: $person2->getPropObject('rights')->setValue($group->getOID());
305:
306: try {
307: $this->assertEquals('user1@test.com,user2@test.com', $person2->getPropObject('rights')->getRelatedClassDisplayFieldValue(), 'testing the getRelatedClassDisplayFieldValue() method on MANY-TO-MANY relation');
308: $this->fail('testing the getRelatedClassDisplayFieldValue() method on MANY-TO-MANY relation');
309: }catch (IllegalArguementException $e) {
310: $this->assertEquals($e->getMessage(), 'Tried to load related MANY-TO-MANY fields but no accessingClassName parameter set on the call to getRelatedClassDisplayFieldValue!', 'testing the getRelatedClassDisplayFieldValue() method on MANY-TO-MANY relation');
311: }
312:
313: $this->assertEquals('user1@test.com,user2@test.com', $person2->getPropObject('rights')->getRelatedClassDisplayFieldValue('RightsObject'), 'testing the getRelatedClassDisplayFieldValue() method on MANY-TO-MANY relation');
314: }
315:
316: 317: 318: 319: 320:
321: public function testGetRelatedClass() {
322: $oneToOneRel = new Relation();
323: $oneToOneRel->setRelatedClass('ArticleCommentObject');
324: $oneToOneRel->setRelatedClassField('articleOID');
325: $oneToOneRel->setRelatedClassDisplayField('content');
326: $oneToOneRel->setRelationType('ONE-TO-ONE');
327:
328: $this->assertEquals('ArticleCommentObject', $oneToOneRel->getRelatedClass(), 'testing the getRelatedClass() method on a ONE-TO-ONE relation');
329:
330: $oneToManyRel = new Relation();
331: $oneToManyRel->setRelatedClass('ArticleCommentObject');
332: $oneToManyRel->setRelatedClassField('articleOID');
333: $oneToManyRel->setRelatedClassDisplayField('content');
334: $oneToManyRel->setRelationType('ONE-TO-MANY');
335:
336: $this->assertEquals('ArticleCommentObject', $oneToManyRel->getRelatedClass(), 'testing the getRelatedClass() method on a ONE-TO-MANY relation');
337:
338: $manyToManyRel = new Relation();
339: $manyToManyRel->setRelatedClass('PersonObject', 'left');
340: $manyToManyRel->setRelatedClassDisplayField('email', 'left');
341: $manyToManyRel->setRelatedClass('RightsObject', 'right');
342: $manyToManyRel->setRelatedClassDisplayField('name', 'right');
343: $manyToManyRel->setRelationType('MANY-TO-MANY');
344:
345: $this->assertEquals('PersonObject', $manyToManyRel->getRelatedClass('left'), 'testing the getRelatedClass() method on a MANY-TO-MANY relation');
346: $this->assertEquals('RightsObject', $manyToManyRel->getRelatedClass('right'), 'testing the getRelatedClass() method on a MANY-TO-MANY relation');
347: }
348:
349: 350: 351: 352: 353:
354: public function testGetSidePass() {
355:
356: $manyToManyRel = new Relation();
357: $manyToManyRel->setRelatedClass('PersonObject', 'left');
358: $manyToManyRel->setRelatedClassDisplayField('email', 'left');
359: $manyToManyRel->setRelatedClass('RightsObject', 'right');
360: $manyToManyRel->setRelatedClassDisplayField('name', 'right');
361: $manyToManyRel->setRelationType('MANY-TO-MANY');
362:
363: $this->assertEquals('left', $manyToManyRel->getSide('PersonObject'), 'testing the getSide() method on a MANY-TO-MANY relation');
364: $this->assertEquals('right', $manyToManyRel->getSide('RightsObject'), 'testing the getSide() method on a MANY-TO-MANY relation');
365: }
366:
367: 368: 369: 370: 371:
372: public function testGetSideFail() {
373:
374: $oneToManyRel = new Relation();
375: $oneToManyRel->setRelatedClass('ArticleCommentObject');
376: $oneToManyRel->setRelatedClassField('articleOID');
377: $oneToManyRel->setRelatedClassDisplayField('content');
378: $oneToManyRel->setRelationType('ONE-TO-MANY');
379:
380: try{
381: $oneToManyRel->getSide('ArticleCommentObject');
382: $this->fail('testing the getSide() method on a ONE-TO-MANY relation');
383: }catch (IllegalArguementException $e) {
384: $this->assertEquals('Error trying to determine the MANY-TO-MANY relationship side for the classname [ArticleCommentObject]', $e->getMessage(), 'testing the getSide() method on a ONE-TO-MANY relation');
385: }
386:
387: }
388:
389: 390: 391: 392: 393:
394: public function testGetRelatedObject() {
395: $oneToOneRel = new Relation();
396: $oneToOneRel->setRelatedClass('PersonObject');
397: $oneToOneRel->setRelatedClassField('OID');
398: $oneToOneRel->setRelatedClassDisplayField('displayName');
399: $oneToOneRel->setRelationType('ONE-TO-ONE');
400: $oneToOneRel->setValue($this->person->getOID());
401:
402: $this->assertEquals($_SESSION['currentUser']->getDisplayName(), $oneToOneRel->getRelatedObject()->get('displayName'), 'testing the getRelatedObject method');
403: }
404:
405: 406: 407: 408: 409:
410: public function testGetRelatedObjects() {
411:
412: $group = new RightsObject();
413: $group->set('name', 'unittestgroup');
414: $group->save();
415:
416: $person1 = new PersonObject();
417: $person1->set('displayName', 'user1');
418: $person1->set('email', 'user1@test.com');
419: $person1->set('password', 'password');
420: $person1->save();
421: $lookup = $person1->getPropObject('rights')->getLookup();
422: $lookup->setValue(array($person1->getOID(), $group->getOID()));
423: $lookup->save();
424:
425: $person2 = new PersonObject();
426: $person2->set('displayName', 'user2');
427: $person2->set('email', 'user2@test.com');
428: $person2->set('password', 'password');
429: $person2->save();
430: $lookup = $person2->getPropObject('rights')->getLookup();
431: $lookup->setValue(array($person2->getOID(), $group->getOID()));
432: $lookup->save();
433:
434: $person2->getPropObject('rights')->setValue($group->getOID());
435:
436: $this->assertEquals(2, count($group->getPropObject('members')->getRelatedObjects('RightsObject')), 'testing the getRelatedObjects method with a MANY-TO-MANY relation');
437: $this->assertTrue($group->getPropObject('members')->getRelatedObjects('RightsObject')[0] instanceof PersonObject, 'testing the getRelatedObjects method with a MANY-TO-MANY relation');
438:
439: $article = new ArticleObject();
440: $article->set('title', 'unit test');
441: $article->set('description', 'unit test');
442: $article->set('content', 'unit test');
443: $article->set('author', 'unit test');
444: $article->save();
445:
446: $comment1 = new ArticleCommentObject();
447: $comment1->set('content', 'unit test');
448: $comment1->getPropObject('articleOID')->setValue($article->getOID());
449: $comment1->save();
450:
451: $comment2 = new ArticleCommentObject();
452: $comment2->set('content', 'unit test');
453: $comment2->getPropObject('articleOID')->setValue($article->getOID());
454: $comment2->save();
455:
456: $this->assertEquals(2, count($article->getPropObject('comments')->getRelatedObjects()), 'testing the getRelatedObjects method with a ONE-TO-MANY relation');
457: $this->assertTrue($article->getPropObject('comments')->getRelatedObjects()[0] instanceof ArticleCommentObject, 'testing the getRelatedObjects method with a ONE-TO-MANY relation');
458: }
459: }
460:
461: ?>