3 defined('AK_TEST_DATABASE_ON') ?
null : define('AK_TEST_DATABASE_ON', true);
4 require_once(dirname(__FILE__
).'/../../../fixtures/config/config.php');
6 class HasAndBelongsToManyTestCase
extends AkUnitTest
11 $this->installAndIncludeModels(array('Post', 'Tag'));
12 $Installer = new AkInstaller();
13 @$Installer->dropTable('posts_tags');
14 @Ak
::file_delete(AK_MODELS_DIR
.DS
.'post_tag.php');
16 $this->installAndIncludeModels(array('Picture', 'Thumbnail','Panorama', 'Property', 'PropertyType'));
19 function test_getAssociatedModelInstance_should_return_a_single_instance() // bug-fix
21 $this->assertReference($this->Post
->tag
->getAssociatedModelInstance(),$this->Post
->tag
->getAssociatedModelInstance());
25 function test_for_has_and_belongs_to_many()
28 $Property =& new Property(array('description'=>'Gandia Palace'));
29 $this->assertEqual($Property->property_type
->getType(), 'hasAndBelongsToMany');
30 $this->assertTrue(is_array($Property->property_types
) && count($Property->property_types
) === 0);
32 $Property->property_type
->load();
33 $this->assertEqual($Property->property_type
->count(), 0);
35 $Chalet =& new PropertyType(array('description'=>'Chalet'));
37 $Property->property_type
->add($Chalet);
38 $this->assertEqual($Property->property_type
->count(), 1);
40 $this->assertReference($Property->property_types
[0], $Chalet);
42 $Property->property_type
->add($Chalet);
43 $this->assertEqual($Property->property_type
->count(), 1);
45 $Condo =& new PropertyType(array('description'=>'Condominium'));
46 $Property->property_type
->add($Condo);
48 $this->assertEqual($Property->property_type
->count(), 2);
50 $this->assertTrue($Property->save());
52 $this->assertFalse($Chalet->isNewRecord());
53 $this->assertFalse($Condo->isNewRecord());
55 $this->assertTrue($Chalet = $Chalet->findFirstBy('description','Chalet', array('include'=>'properties')));
56 $this->assertEqual($Chalet->properties
[0]->getId(), $Property->getId());
58 $this->assertTrue($Condo = $Condo->findFirstBy('description','Condominium', array('include'=>'properties')));
59 $this->assertEqual($Condo->properties
[0]->getId(), $Property->getId());
61 $this->assertReference($Chalet, $Property->property_types
[0]);
62 $this->assertReference($Condo, $Property->property_types
[1]);
64 $Property =& new Property($Property->getId());
65 $Property->property_type
->load();
67 $this->assertEqual($Property->property_type
->association_id
, 'property_types');
68 $this->assertEqual($Property->property_type
->count(), 2);
70 $Property->property_types
= array();
71 $this->assertEqual($Property->property_type
->count(), 0);
73 $Property->property_type
->load();
74 $this->assertEqual($Property->property_type
->count(), 0);
76 $Property->property_type
->load(true);
77 $this->assertEqual($Property->property_type
->count(), 2);
79 $this->assertEqual($Property->property_types
[1]->getType(), 'PropertyType');
81 $Property->property_type
->delete($Property->property_types
[1]);
83 $this->assertEqual($Property->property_type
->count(), 1);
85 $Property->property_type
->load(true);
86 $this->assertEqual($Property->property_type
->count(), 1);
89 $Property =& $Property->findFirstBy('description','Gandia Palace');
90 $PropertyType =& new PropertyType();
92 $PropertyTypes =& $PropertyType->find();
94 $Property->property_type
->set($PropertyTypes);
96 $this->assertEqual($Property->property_type
->count(), count($PropertyTypes));
98 $Property =& $Property->findFirstBy('description','Gandia Palace');
100 $Property->property_type
->load();
101 $this->assertEqual($Property->property_type
->count(), count($PropertyTypes));
103 $Property =& $Property->findFirstBy('description','Gandia Palace');
105 $PropertyType->set('description', 'Palace');
107 $Property->property_type
->set($PropertyType);
109 $this->assertEqual($Property->property_type
->count(), 1);
111 $this->assertTrue(in_array('property_types', $Property->getAssociatedIds()));
113 $Property = $Property->findFirstBy('description','Gandia Palace',array('include'=>'property_types'));
115 $this->assertIdentical($Property->property_type
->count(), 1);
117 $this->assertTrue($Property->property_type
->delete($Property->property_types
[0]));
119 $this->assertIdentical($Property->property_type
->count(), 0);
121 $Property = $Property->findFirstBy('description','Gandia Palace');
122 $this->assertIdentical($Property->property_type
->count(), 0);
124 // It should return existing Property even if it doesnt have property_types
125 $this->assertTrue($Property->findFirstBy('description','Gandia Palace',array('include'=>'property_types')));
127 $Property =& new Property(array('description'=> 'Luxury Downtown House'));
128 $Apartment =& $PropertyType->create(array('description'=>'Apartment'));
129 $Loft =& $PropertyType->create(array('description'=>'Loft'));
130 $Penthouse =& $PropertyType->create(array('description'=>'Penthouse'));
132 $Property->property_type
->setByIds(array($Apartment->getId(),$Loft->getId(),$Penthouse->getId()));
134 $this->assertEqual($Property->property_type
->count(), 3);
136 $this->assertTrue($Property->save());
137 $this->assertTrue($Property->save());
139 $this->assertTrue($Property =& $Property->findFirstBy('description', 'Luxury Downtown House'));
141 $Property->property_type
->load();
143 $this->assertEqual($Property->property_type
->count(), 3);
145 $FoundApartment = $Property->property_type
->find('first', array('description'=>'Apartment'));
146 $this->assertEqual($Apartment->get('description').$Apartment->getId(), $FoundApartment->get('description').$FoundApartment->getId());
148 $FoundTypes = $Property->property_type
->find();
150 $this->assertEqual(count($FoundTypes), $Property->property_type
->count());
152 $descriptions = array();
153 foreach ($FoundTypes as $FoundType){
154 $descriptions[] = $FoundType->get('description');
158 $this->assertEqual($descriptions, array('Apartment','Loft','Penthouse'));
160 $this->assertFalse($Property->property_type
->isEmpty());
162 $this->assertEqual($Property->property_type
->getSize(), 3);
164 $this->assertTrue($Property->property_type
->clear());
166 $this->assertTrue($Property->property_type
->isEmpty());
168 $this->assertEqual($Property->property_type
->getSize(), 0);
170 $Property =& new Property();
172 $LandProperty =& $Property->property_type
->build(array('description'=>'Land'));
174 $this->assertReference($LandProperty, $Property->property_types
[0]);
176 $this->assertTrue($Property->property_types
[0]->isNewRecord());
178 $this->assertEqual($LandProperty->getType(), 'PropertyType');
180 $Property->set('description', 'Plot of Land in Spain');
182 $this->assertTrue($Property->save());
184 $this->assertTrue($LandProperty = $Property->findFirstBy('description', 'Plot of Land in Spain', array('include'=>'property_types')));
186 $this->assertEqual($LandProperty->property_types
[0]->get('description'), 'Land');
188 $Property =& new Property(array('description'=>'Seaside house in Altea'));
189 $SeasidePropertyType =& $Property->property_type
->create(array('description'=>'Seaside property'));
190 $this->assertReference($SeasidePropertyType, $Property->property_types
[0]);
191 $this->assertTrue($SeasidePropertyType->isNewRecord());
193 $Property =& new Property(array('description'=>'Bermi\'s appartment in Altea'));
194 $this->assertTrue($Property->save());
195 $SeasidePropertyType =& $Property->property_type
->create(array('description'=>'Seaside property'));
196 $this->assertReference($SeasidePropertyType, $Property->property_types
[0]);
197 $this->assertFalse($SeasidePropertyType->isNewRecord());
199 $this->assertTrue($PropertyInAltea = $Property->findFirstBy('description', 'Bermi\'s appartment in Altea', array('include'=>'property_types')));
201 $this->assertEqual($PropertyInAltea->property_types
[0]->get('description'), 'Seaside property');
204 // Testing destroy callbacks
205 $this->assertTrue($Property =& $Property->findFirstBy('description', 'Bermi\'s appartment in Altea'));
206 $property_id = $Property->getId();
207 //echo '<pre>'.print_r($Property->_associations, true).'</pre>';
209 $this->assertTrue($Property->destroy());
211 $RecordSet = $PropertyInAltea->_db
->execute('SELECT * FROM properties_property_types WHERE property_id = '.$property_id);
212 $this->assertEqual($RecordSet->RecordCount(), 0);
216 function test_find_on_unsaved_models_including_associations()
218 $Property =& new Property('description->','Chalet by the sea');
220 $PropertyType =& new PropertyType();
221 $this->assertTrue($PropertyTypes = $PropertyType->findAll());
222 $Property->property_type
->add($PropertyTypes);
223 $this->assertTrue($Property->save());
225 $Property =& new Property();
228 foreach (array_keys($PropertyTypes) as $k){
229 $expected[] = $PropertyTypes[$k]->get('description');
232 $this->assertTrue($Properties = $Property->findFirstBy('description', 'Chalet by the sea', array('include'=>'property_type')),'Finding including habtm associated from a new object doesn\'t work');
234 foreach (array_keys($Properties->property_types
) as $k){
235 $this->assertTrue(in_array($Properties->property_types
[$k]->get('description'),$expected));
240 function test_clean_up_dependencies()
242 $Property =& new Property('description->','Luxury Estate');
243 $PropertyType =& new PropertyType();
244 $this->assertTrue($PropertyType =& $PropertyType->create(array('description'=>'Mansion')));
245 $Property->property_type
->add($PropertyType);
246 $this->assertTrue($Property->save());
248 $PropertyType =& $PropertyType->findFirstBy('description','Mansion');
249 $PropertyType->property
->load();
250 $this->assertEqual($PropertyType->properties
[0]->getId(), $Property->getId());
251 $this->assertEqual($PropertyType->property
->count(), 1);
253 $this->assertTrue($Property->destroy());
256 $PropertyType =& $PropertyType->findFirstBy('description','Mansion');
257 $PropertyType->property
->load();
258 $this->assertTrue(empty($PropertyType->properties
[0]));
259 $this->assertEqual($PropertyType->property
->count(), 0);
264 function test_double_assignation()
266 $AkelosOffice =& new Property(array('description'=>'Akelos new Office'));
267 $this->assertTrue($AkelosOffice->save());
269 $PalafollsOffice =& new Property(array('description'=>"Bermi's home office"));
270 $this->assertTrue($PalafollsOffice->save());
272 $CoolOffice =& new PropertyType(array('description'=>'Cool office'));
273 $this->assertTrue($CoolOffice->save());
275 $AkelosOffice->property_type
->add($CoolOffice);
276 $this->assertEqual($CoolOffice->property
->count(), 1);
278 $PalafollsOffice->property_type
->add($CoolOffice);
279 $this->assertEqual($CoolOffice->property
->count(), 2);
283 function test_scope_for_multiple_member_deletion()
285 $PisoJose =& new Property('description->','Piso Jose');
286 $PisoBermi =& new Property('description->','Piso Bermi');
288 $Atico =& new PropertyType('description->','Ático');
289 $Apartamento =& new PropertyType('description->','Apartamento');
291 $this->assertTrue($PisoJose->save() && $PisoBermi->save() && $Atico->save() && $Apartamento->save());
293 $PisoJose->property_type
->add($Atico);
294 $PisoJose->property_type
->add($Apartamento);
296 $PisoBermi->property_type
->add($Atico);
297 $PisoBermi->property_type
->add($Apartamento);
300 $this->assertTrue($PisoJose =& $PisoJose->findFirstBy('description','Piso Jose'));
301 $this->assertTrue($Atico =& $Atico->findFirstBy('description','Ático'));
303 $PisoJose->property_type
->load();
305 $PisoJose->property_type
->delete($Atico);
307 $this->assertTrue($PisoBermi =& $PisoBermi->findFirstBy('description','Piso Bermi'));
309 $this->assertTrue($PisoJose =& $PisoJose->findFirstBy('description','Piso Jose'));
310 $PisoJose->property_type
->load();
312 $this->assertTrue($Atico =& $Atico->findFirstBy('description','Ático'));
313 $this->assertTrue($Apartamento =& $Apartamento->findFirstBy('description','Apartamento'));
315 $this->assertEqual($PisoJose->property_types
[0]->getId(), $Apartamento->getId());
316 $this->assertEqual($PisoBermi->property_type
->count(), 2);
322 function test_associated_uniqueness()
324 $Property =& new Property();
325 $PropertyType =& new PropertyType();
327 $this->assertTrue($RanchoMaria =& $Property->create(array('description'=>'Rancho Maria')));
328 $this->assertTrue($Rancho =& $PropertyType->create(array('description'=>'Rancho')));
330 $Rancho->property
->load();
331 $this->assertEqual($Rancho->property
->count(), 0);
332 $Rancho->property
->add($RanchoMaria);
333 $this->assertEqual($Rancho->property
->count(), 1);
335 $this->assertTrue($RanchoMaria =& $Property->findFirstBy('description','Rancho Maria'));
336 $this->assertTrue($Rancho =& $PropertyType->findFirstBy('description','Rancho', array('include'=>'properties')));
338 $Rancho->property
->add($RanchoMaria);
339 $this->assertEqual($Rancho->property
->count(), 1);
341 $Rancho->set('description', 'Rancho Type');
342 $this->assertTrue($Rancho->save());
343 $this->assertTrue($Rancho =& $PropertyType->findFirstBy('description','Rancho Type', array('include'=>'properties')));
344 $this->assertEqual($Rancho->property
->count(), 1);
347 function test_should_include_associates_using_simple_finder()
349 $Property =& new Property();
350 $PropertyType =& new PropertyType();
351 $this->assertTrue($Rancho =& $PropertyType->findFirstBy('description','Rancho Type', array('include'=>'properties')));
353 $this->assertTrue($RanchoMaria =& $Property->find($Rancho->properties
[0]->getId(), array('include'=>'property_types')));
355 $this->assertEqual($RanchoMaria->property_types
[0]->getId(), $Rancho->getId());
356 $this->assertEqual($RanchoMaria->getId(), $Rancho->properties
[0]->getId());
360 function test_should_remove_associated_using_the_right_key()
362 $Installer =& new AkInstaller();
363 @$Installer->dropTable('groups_users');
364 @Ak
::file_delete(AK_MODELS_DIR
.DS
.'group_user.php');
366 $this->installAndIncludeModels('User', 'Group', array('instantiate' => true));
368 $Admin =& $this->Group
->create(array('name' => 'Admin'));
370 $Moderator =& $this->Group
->create(array('name' => 'Moderator'));
372 $this->assertFalse($Admin->hasErrors());
373 $this->assertFalse($Moderator->hasErrors());
375 $Salavert =& $this->User
->create(array('name' => 'Jose'));
376 $this->assertFalse($Salavert->hasErrors());
378 $Salavert->group
->setByIds($Admin->getId(), $Moderator->getId());
380 $Salavert =& $this->User
->find($Salavert->getId());
381 $this->assertEqual(2, $Salavert->group
->count());
383 $Jyrki =& $this->User
->create(array('name' => 'Jyrki'));
384 $this->assertFalse($Jyrki->hasErrors());
385 $Jyrki->group
->setByIds($Admin->getId(), $Moderator->getId());
386 $Jyrki =& $this->User
->find($Jyrki->getId());
387 $this->assertEqual(2, $Jyrki->group
->count());
390 $Salavert =& $this->User
->find($Salavert->getId());
391 $this->assertEqual(2, $Salavert->group
->count());
396 function test_remove_existing_associates_before_setting_by_id()
399 foreach (range(1,10) as $i){
400 $Post =& new Post(array('title' => 'Post '.$i));
401 $Post->tag
->create(array('name' => 'Tag '.$i));
402 $this->assertTrue($Post->save());
403 $this->assertEqual($Post->tag
->count(), 1); // dont know why but this fails sometimes, randomly -kaste
406 $Post11 =& new Post(array('name' => 'Post 11'));
407 $this->assertTrue($Post11->save());
409 $Post->tag
->setByIds(1,2,3,4,5);
411 $this->assertTrue($Post =& $Post->find(10, array('include' => 'tags','order' => '_tags.id ASC')));
413 foreach (array_keys($Post->tags
) as $k){
414 $this->assertEqual($Post->tags
[$k]->getId(), $k+
1);
417 // Tag 10 should exist but unrelated to a post
418 $this->assertTrue($Tag =& $Post->tags
[$k]->find(10));
419 $this->assertEqual($Tag->post
->count(), 0);
421 $Post11->tag
->setByIds(array(10,1));
423 $this->assertTrue($Tag =& $Tag->find(10, array('include'=>'posts')));
424 $this->assertEqual($Tag->posts
[0]->getId(), 11);
431 ak_test('HasAndBelongsToManyTestCase',true);