3 if(!defined('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION')){
4 define('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION',false);
7 defined('AK_TEST_DATABASE_ON') ?
null : define('AK_TEST_DATABASE_ON', true);
8 require_once(dirname(__FILE__
).'/../../../fixtures/config/config.php');
10 class test_AkActiveRecord_belongsTo_Associations
extends AkUnitTest
15 $this->installAndIncludeModels(array('Picture', 'Thumbnail','Panorama', 'Property', 'PropertyType', 'Person', 'Account'));
19 function test_for_single_has_one_association()
21 $Picture =& new Picture(array('title'=>'The Akelos Media Team at SIMO'));
22 $this->assertReference($Picture, $Picture->hasOne
->Owner
);
23 $this->assertEqual($Picture->main_thumbnail
->getAssociationId(), 'main_thumbnail');
24 $this->assertEqual($Picture->main_thumbnail
->getType(), 'hasOne');
25 $this->assertFalse($Picture->main_thumbnail
->getId());
27 $Thumbnail =& new Thumbnail(array('caption'=>'SIMO 2005'));
29 $Picture->main_thumbnail
->assign($Thumbnail);
31 $this->assertEqual($Picture->main_thumbnail
->getAssociationId(), 'main_thumbnail');
32 $this->assertEqual($Picture->main_thumbnail
->getType(), 'Thumbnail');
33 $this->assertEqual($Picture->main_thumbnail
->getAssociationType(), 'hasOne');
34 $this->assertFalse($Picture->main_thumbnail
->getId());
36 $this->assertTrue($Picture->save());
37 $this->assertFalse($Picture->isNewRecord());
38 $this->assertFalse($Thumbnail->isNewRecord());
40 $this->assertReference($Thumbnail, $Picture->main_thumbnail
);
42 $SimoPic = $Picture->findFirstBy('title:has','SIMO');
43 $this->assertTrue(empty($SimoPic->main_thumbnail
->caption
));
45 $this->assertEqual($SimoPic->main_thumbnail
->getAssociatedType(), 'hasOne');
47 $this->assertEqual($Picture->main_thumbnail
->getAssociatedType(), 'hasOne');
49 $SimoPic = $Picture->findFirstBy('title:has','SIMO', array('include'=>'main_thumbnail'));
51 $this->assertEqual($SimoPic->title
, 'The Akelos Media Team at SIMO');
52 $this->assertEqual($SimoPic->main_thumbnail
->caption
, 'SIMO 2005');
54 $Picture =& new Picture(array('title'=>'The Akelos Media Team at CeBIT'));
55 $Picture->main_thumbnail
->build(array('caption' => 'CeBIT 2005'));
56 $this->assertTrue($Picture->save());
57 $this->assertFalse($Picture->isNewRecord());
58 $this->assertFalse($Picture->main_thumbnail
->isNewRecord());
60 $CeBitPic =& $Picture->findFirstBy('title:has','CeBIT', array('include'=>'main_thumbnail'));
62 $this->assertEqual($CeBitPic->title
, 'The Akelos Media Team at CeBIT');
63 $this->assertEqual($CeBitPic->main_thumbnail
->caption
, 'CeBIT 2005');
65 $Picture =& new Picture(array('title'=>'The Akelos Media Team at Carlet'));
67 $this->assertTrue($Picture->save());
69 $this->assertFalse($Picture->findFirstBy('title:has','Carlet', array('include'=>'main_thumbnail')));
71 $this->assertTrue($CarletPic =& $Picture->findFirstBy('title:has','Carlet', array('include'=>array('main_thumbnail'=>array('conditions'=>false)))));
72 $this->assertEqual($CarletPic->title
,'The Akelos Media Team at Carlet');
75 $this->assertReference($CarletPic->main_thumbnail
->_AssociationHandler
->Owner
, $CarletPic);
77 $CarletPic =& $Picture->findFirstBy('title:has','Carlet');
80 $this->assertReference($CarletPic->main_thumbnail
->_AssociationHandler
->Owner
, $CarletPic);
82 $this->assertEqual($CarletPic->title
, 'The Akelos Media Team at Carlet');
83 $this->assertEqual($CarletPic->main_thumbnail
->getType(), 'hasOne');
85 $CarletPic->main_thumbnail
->create(array('caption'=>'Carlet'));
88 $this->assertReference($CarletPic->main_thumbnail
->_AssociationHandler
->Owner
, $CarletPic);
90 $this->assertFalse($CarletPic->main_thumbnail
->isNewRecord());
92 $CarletPic =& $Picture->findFirstBy('title:has','Carlet', array('include'=>'main_thumbnail'));
93 $this->assertEqual($CarletPic->main_thumbnail
->caption
, 'Carlet');
96 $this->assertReference($CarletPic->main_thumbnail
->_AssociationHandler
->Owner
, $CarletPic);
98 $this->assertTrue($SimoPic->destroy());
100 $this->assertFalse($Picture->findFirstBy('title:has','SIMO', array('include'=>'main_thumbnail')));
102 $this->assertFalse($Thumbnail->findFirstBy('caption','SIMO 2005'));
104 $Thumbnail =& new Thumbnail(array('caption'=>'Our Office'));
107 $this->assertReference($CarletPic->main_thumbnail
->_AssociationHandler
->Owner
, $CarletPic);
109 $this->assertReference($CarletPic->main_thumbnail
->replace($Thumbnail), $Thumbnail);
110 $this->assertReference($CarletPic->main_thumbnail
, $Thumbnail);
111 //$this->assertReference($CarletPic->prueba, $Thumbnail);
113 $this->assertTrue($CarletPic->save());
115 $this->assertEqual($CarletPic->main_thumbnail
->caption
, 'Our Office');
117 $this->assertFalse($Thumbnail->findFirstBy('caption','Carlet'));
119 $this->assertTrue($OfficeThumbnail =& $Thumbnail->findFirstBy('caption', 'Our Office'));
120 $this->assertEqual($OfficeThumbnail->getId(), $CarletPic->main_thumbnail
->getId());
121 $Thumbnail =& new Thumbnail(array('caption'=>'Lucky (our pet)'));
123 $CarletPic->main_thumbnail
->replace($Thumbnail);
124 $this->assertTrue($CarletPic->save());
126 $CarletPic =& $Picture->findFirstBy('title:has','Carlet', array('include'=>'main_thumbnail'));
127 $this->assertEqual($CarletPic->main_thumbnail
->caption
, 'Lucky (our pet)');
129 $CarletPic =& $Picture->findFirstBy('title:has','Carlet');
130 $CarletPic->main_thumbnail
->load();
131 $this->assertEqual($CarletPic->main_thumbnail
->caption
, 'Lucky (our pet)');
133 $this->assertFalse($Thumbnail->findFirstBy('caption','Our Office'));
137 function test_for_belongs_to_association()
139 $Thumbnail =& new Thumbnail();
140 $Thumbnail =& $Thumbnail->findFirstBy('caption','Lucky (our pet)');
141 $Thumbnail =& new Thumbnail($Thumbnail->getId());
143 $this->assertEqual($Thumbnail->picture
->getType(), 'belongsTo');
145 $Thumbnail =& $Thumbnail->findFirstBy('caption:has','Lucky', array('include'=>'picture'));
147 $this->assertEqual($Thumbnail->picture
->getType(), 'Picture');
148 $this->assertEqual($Thumbnail->picture
->title
, 'The Akelos Media Team at Carlet');
150 $Alicia =& $Thumbnail->create('caption->','Alicia');
151 $this->assertTrue(!$Alicia->isNewRecord());
153 $this->assertEqual($Alicia->picture
->getType(), 'belongsTo');
155 $MyGirl =& new Picture(array('title'=>'Alicia Sadurní'));
157 $Alicia->picture
->assign($MyGirl);
159 $this->assertEqual($Alicia->picture
->getType(), 'Picture');
161 $this->assertReference($Alicia->picture
, $MyGirl);
162 $this->assertFalse($MyGirl->isNewRecord());
163 $this->assertEqual($Alicia->get('photo_id'), $MyGirl->getId());
164 $this->assertTrue($Alicia->save());
166 $Thumbnail =& new Thumbnail();
167 $Thumbnail->caption
= 'Party 2005';
169 $Picture =& $Thumbnail->picture
->build(array('title'=>'Akelos Party 2005'));
170 $this->assertReference($Thumbnail->picture
, $Picture);
171 $this->assertEqual($Picture->getType(), 'Picture');
173 $this->assertTrue($Picture->isNewRecord() && $Thumbnail->isNewRecord());
175 $this->assertTrue($Thumbnail->save());
177 $this->assertFalse($Picture->isNewRecord());
178 $this->assertFalse($Thumbnail->isNewRecord());
181 $Thumbnail =& new Thumbnail();
182 $Thumbnail->caption
= 'Party 2006';
184 $Picture =& $Thumbnail->picture
->create(array('title'=>'Akelos Party 2006'));
185 $this->assertReference($Thumbnail->picture
, $Picture);
186 $this->assertEqual($Picture->getType(), 'Picture');
187 $this->assertFalse($Picture->isNewRecord());
188 $this->assertTrue($Thumbnail->isNewRecord());
191 $Thumbnail =& new Thumbnail(array('title'=>'Akelos new office'));
192 $Thumbnail->loadAssociations();
193 $Thumbnail->picture
->assign($Picture);
194 $this->assertTrue($Thumbnail->save());
196 $this->assertEqual($Thumbnail->photo_id
, $Picture->id
);
200 function test_for_multiple_hasone_and_belongsto()
202 $Altea =& new Picture(array('title'=>'Altea Cupula de Mediterraneo, Costa Blanca'));
203 $Altea->main_thumbnail
->build(array('caption'=>'Altea'));
204 $this->assertTrue($Altea->main_thumbnail
->isNewRecord());
205 $this->assertEqual($Altea->main_thumbnail
->getType(), 'Thumbnail');
206 $this->assertTrue($Altea->save());
207 $this->assertFalse($Altea->main_thumbnail
->isNewRecord());
210 $Altea =& new Picture(array('title'=>'Altea2'));
211 $Altea->main_thumbnail
->create(array('caption'=>'Altea2'));
212 $this->assertFalse($Altea->main_thumbnail
->isNewRecord());
213 $this->assertEqual($Altea->main_thumbnail
->getType(), 'Thumbnail');
214 $this->assertTrue($Altea->save());
216 $Altea =& new Picture(array('title'=>'Altea3'));
217 $Altea->main_thumbnail
->assign(new Thumbnail(array('caption'=>'Altea3')));
219 $this->assertTrue($Altea->main_thumbnail
->isNewRecord());
220 $this->assertEqual($Altea->main_thumbnail
->getType(), 'Thumbnail');
221 $this->assertTrue($Altea->save());
223 $this->assertFalse($Altea->main_thumbnail
->isNewRecord());
225 $Altea->main_thumbnail
->replace(new Thumbnail(array('caption'=>'3rd Altea pic')));
226 $this->assertFalse($Altea->main_thumbnail
->isNewRecord());
228 $Thumbnail = new Thumbnail();
229 $this->assertFalse($Thumbnail->findFirstBy('caption','Altea3'));
231 $Panorama =& new Panorama(array('title'=>'Views from the old town'));
232 $this->assertTrue($Panorama->save());
233 $Panorama->thumbnail
->build(array('caption'=>'Altea paronamic views from the Old town'));
234 $this->assertEqual($Panorama->thumbnail
->getType(), 'Thumbnail');
235 $this->assertTrue($Panorama->thumbnail
->isNewRecord());
236 $this->assertTrue($Panorama->save());
237 $this->assertFalse($Panorama->thumbnail
->isNewRecord());
239 $Thumbnail =& new Thumbnail();
240 $Thumbnail = $Thumbnail->findFirstBy('caption:has', 'Old town', array('include'=>'panorama'));
241 $this->assertEqual($Thumbnail->panorama
->title
, 'Views from the old town');
245 function test_primary_key_setting()
247 $Hilario =& new Person('first_name->','Hilario','last_name->','Hervás','email->','hilario@example.com');
248 $Jose =& new Person('first_name->','Jose','last_name->','Salavert','email->','salavert@example.com');
249 $Vero =& new Person('first_name->','Vero','last_name->','Machí','email->','vero@example.com');
250 $Bermi =& new Person('first_name->','Bermi','last_name->','Ferrer','email->','bermi@example.com');
252 $this->assertTrue($Hilario->save() && $Bermi->save());
254 $BermisAccount =& new Account('username->','bermi','password->','pass');
255 $Bermi->account
->assign($BermisAccount);
257 $this->assertEqual($BermisAccount->person_id
,$Bermi->id
);
259 $SalavertsAccount =& new Account('username->','salavert','password->','pass');
260 $Jose->account
->assign($SalavertsAccount);
264 $this->assertEqual($SalavertsAccount->person_id
,$Jose->id
);
266 $VerosAccount =& new Account('username->','vero','password->','pass');
268 $this->assertTrue($VerosAccount->save());
270 $VerosAccount->person
->assign($Vero);
272 $VerosAccount->save();
273 $this->assertEqual($VerosAccount->person_id
, $Vero->id
);
275 $HilariosAccount =& new Account('username->','hilario','password->','pass');
276 $Hilario->account
->assign($HilariosAccount);
279 $this->assertEqual($HilariosAccount->id
, $Hilario->account
->id
);
281 $Hilario =& $Hilario->findFirstBy('first_name','Hilario');
282 $Hilario->account
->load();
284 $this->assertEqual($HilariosAccount->id
, $Hilario->account
->id
);
288 function test_should_load_resquested_list()
290 $this->installAndIncludeModels(array('TodoList', 'TodoTask'));
292 $ListA =& new TodoList(array('name' => 'A'));
293 $this->assertTrue($ListA->save());
295 $ListB =& new TodoList(array('name' => 'B'));
296 $this->assertTrue($ListB->save());
298 $Task1 =& $ListB->task
->create(array('details' => 1));
300 $Task1->todo_list
->load(true);
302 $this->assertEqual($Task1->todo_list
->getId(), $ListB->getId());
308 ak_test('test_AkActiveRecord_belongsTo_Associations', true);