Changeset 328

Show
Ignore:
Timestamp:
08/27/07 20:10:14 (1 year ago)
Author:
bermiferrer
Message:

Adding more tests for habtm and has_many associations. Fixed invalid associate selection when removing more than one associated.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/AkActiveRecord/AkHasAndBelongsToMany.php

    r322 r328  
    440440 
    441441                if(!in_array($id, $ids_to_skip)){ 
    442                     $JoinObjectToDelete =& $this->JoinObject->findFirstBy( 
    443                         $options['foreign_key'].' AND '.$options['association_foreign_key'],  
    444                         $this->Owner->getId(), $id); 
    445                     if($JoinObjectToDelete->destroy()){ 
    446                         $items_to_remove_from_collection[] = $id; 
    447                     }else{ 
    448                         $success = false; 
     442                 
     443                    if($JoinObjectsToDelete =& $this->JoinObject->findAllBy($options['foreign_key'].' AND '.$options['association_foreign_key'], $this->Owner->getId(), $id)){ 
     444                        foreach (array_keys($JoinObjectsToDelete) as $k) { 
     445                            if($JoinObjectsToDelete[$k]->destroy()){ 
     446                                $items_to_remove_from_collection[] = $id; 
     447                            }else{ 
     448                                $success = false; 
     449                            } 
     450                        } 
    449451                    } 
    450452                } 
  • trunk/lib/AkActiveRecord/AkHasMany.php

    r315 r328  
    252252        $ids = func_get_args(); 
    253253        $ids = is_array($ids[0]) ? $ids[0] : $ids; 
     254         
    254255        $AssociatedModel =& $this->getAssociatedModelInstance(); 
    255256        if(!empty($ids)){ 
     
    314315                        break; 
    315316                        case 'nullify': 
    316                         $ids_to_nullify[] = $Associated[$k]->quotedId(); 
     317                            $id_to_nullify = $Associated[$k]->quotedId(); 
     318                            if(!empty($id_to_nullify)){ 
     319                                $ids_to_nullify[] = $id_to_nullify; 
     320                            } 
    317321                        default: 
    318322                        break; 
     
    321325            } 
    322326 
     327            $ids_to_nullify = empty($ids_to_nullify) ? false : array_diff($ids_to_nullify,array('')); 
    323328            if(!empty($ids_to_nullify)){ 
    324329                $success = $AssociatedModel->updateAll( 
     
    399404            $Member->__hasManyMemberId = Ak::randomString(); 
    400405        } 
    401         $object_id = $Member->getId()
     406        $object_id = method_exists($Member,'getId') ? $Member->getId() : null
    402407        if(!empty($object_id)){ 
    403408            $this->asssociated_ids[$object_id] = $Member->__hasManyMemberId; 
     
    429434    { 
    430435        if(!$this->Owner->isNewRecord()){ 
    431             $foreign_key = $this->getOption($this->association_id, 'foreign_key'); 
    432             if($this->getOption($this->association_id, 'class_name') != $Associated->getModelName() || $foreign_key == $Associated->get($foreign_key)){ 
    433                 return false; 
    434             } 
    435             $Associated->set($foreign_key, $this->Owner->getId()); 
    436             return true; 
     436            if(method_exists($Associated, 'getModelName')){ 
     437                $foreign_key = $this->getOption($this->association_id, 'foreign_key'); 
     438                if($this->getOption($this->association_id, 'class_name') != $Associated->getModelName() || $foreign_key == $Associated->get($foreign_key)){ 
     439                    return false; 
     440                } 
     441                $Associated->set($foreign_key, $this->Owner->getId()); 
     442                return true; 
     443            } 
    437444        } 
    438445        return false; 
     
    710717 
    711718                        case 'nullify': 
    712                         $ids_to_nullify[] = $object->{$k}[$key]->quotedId(); 
     719                        $id_to_nullify = $object->{$k}[$key]->quotedId(); 
     720                        if(!empty($id_to_nullify)){ 
     721                            $ids_to_nullify[] = $id_to_nullify; 
     722                        } 
    713723                        break; 
    714724 
     
    717727                    } 
    718728                } 
    719  
     729                 
     730                $ids_to_nullify = empty($ids_to_nullify) ? false : array_diff($ids_to_nullify,array('')); 
    720731                if(!empty($ids_to_nullify)){ 
    721732                    $success = $object->{$k}[$key]->updateAll( 
    722733                    ' '.$object->$v->options[$k]['foreign_key'].' = NULL ', 
    723                     ' '.$object->$v->options[$k]['foreign_key'].' = '.$object->quotedId().' AND '.$object->{$k}[$key]->getPrimaryKey().' IN ('.join(', ',$ids_to_nullify).')' 
     734                    ' '.$object->$v->options[$k]['foreign_key'].' = '.$object->quotedId().' AND '.$object->{$k}[$key]->getPrimaryKey().' IN ('.join(', ', $ids_to_nullify).')' 
    724735                    ) ? $success : false; 
    725736                }elseif(!empty($ids_to_delete)){ 
  • trunk/script/test

    r325 r328  
    2222 
    2323defined('AK_TEST_DATABASE_ON') ? null : define('AK_TEST_DATABASE_ON', true); 
     24defined('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION') ? null : define('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION', false); 
     25 
    2426require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'test'.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php'); 
    2527 
     
    4244                    if(!preg_match('/^('.join('|',$____skip_tests).')$/i',$match[1])){ 
    4345                        $____skip_tests[] = $match[1]; 
    44                         Ak::trace(join(',',$____skip_tests)); 
    4546                        ak_test($match[1].'TestCase', true); 
    4647                    } 
  • trunk/test/fixtures/app/installers/comment_installer.php

    r315 r328  
    1414    function down_1() 
    1515    { 
    16         $this->dropTable('comments'); 
     16        $this->dropTable('comments', array('sequence'=>true)); 
    1717    } 
    1818     
  • trunk/test/fixtures/app/installers/post_installer.php

    r313 r328  
    1010    function uninstall() 
    1111    { 
    12         $this->dropTable('posts'); 
     12        $this->dropTable('posts', array('sequence'=>true)); 
    1313    } 
    1414} 
  • trunk/test/fixtures/app/models/post.php

    r315 r328  
    44{ 
    55    var $has_many = 'comments'; 
     6    var $habtm = 'tags'; 
    67} 
    78 
  • trunk/test/fixtures/app/models/tag.php

    r55 r328  
    77            'join_table' => 'taggings', 
    88            'join_class_name' => 'Tagging' 
    9         ) 
     9        ), 
     10        'posts' 
    1011    ); 
    1112     
  • trunk/test/unit/lib/AkActiveRecord/AkHasAndBelongsToMany.php

    r320 r328  
    11<?php 
    22 
    3 if(!defined('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION')){ 
    4     define('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION',false); 
    5 
    6  
    7 defined('AK_TEST_DATABASE_ON') ? null : define('AK_TEST_DATABASE_ON', true); 
    8 require_once(dirname(__FILE__).'/../../../fixtures/config/config.php'); 
    9  
    10  
    11 class test_AkActiveRecord_hasAndBelongsToMany_Associations extends  AkUnitTest 
     3class HasAndBelongsToManyTestCase extends  AkUnitTest 
    124{ 
     5    function setup() 
     6    { 
     7        $this->installAndIncludeModels(array('Post', 'Tag')); 
     8        $Installer = new AkInstaller(); 
     9        @$Installer->dropTable('posts_tags'); 
     10        @Ak::file_delete(AK_MODELS_DIR.DS.'post_tag.php'); 
     11    } 
     12    /**/ 
    1313    function test_start() 
    1414    { 
     
    2222        $installer->uninstall(); 
    2323        $installer->install(); 
     24         
     25         
    2426        $models = array('Picture', 'Thumbnail','Panorama', 'Property', 'PropertyType'); 
    2527        foreach ($models as $model){ 
     
    98100        $this->assertEqual($Property->property_type->count(), 1); 
    99101 
    100         $Property = $Property->findFirstBy('description','Gandia Palace'); 
    101  
    102         $PropertyType = new PropertyType(); 
    103  
    104         $PropertyTypes = $PropertyType->find(); 
     102         
     103        $Property =& $Property->findFirstBy('description','Gandia Palace'); 
     104        $PropertyType =& new PropertyType(); 
     105 
     106        $PropertyTypes =& $PropertyType->find(); 
    105107 
    106108        $Property->property_type->set($PropertyTypes); 
     
    108110        $this->assertEqual($Property->property_type->count(), count($PropertyTypes)); 
    109111 
    110         $Property = $Property->findFirstBy('description','Gandia Palace'); 
     112        $Property =& $Property->findFirstBy('description','Gandia Palace'); 
    111113 
    112114        $Property->property_type->load(); 
    113115        $this->assertEqual($Property->property_type->count(), count($PropertyTypes)); 
    114116 
    115         $Property = $Property->findFirstBy('description','Gandia Palace'); 
     117        $Property =& $Property->findFirstBy('description','Gandia Palace'); 
    116118 
    117119        $PropertyType->set('description', 'Palace'); 
     120 
    118121        $Property->property_type->set($PropertyType); 
    119122 
    120123        $this->assertEqual($Property->property_type->count(), 1); 
    121  
     124         
    122125        $this->assertTrue(in_array('property_types', $Property->getAssociatedIds())); 
    123126 
     
    224227 
    225228    } 
    226  
    227  
     229     
    228230    function test_find_on_unsaved_models_including_associations() 
    229231    { 
     
    404406 
    405407    } 
     408 
     409 
     410    function test_remove_existing_associates_before_setting_by_id() 
     411    { 
     412 
     413        foreach (range(1,10) as $i){ 
     414            $Post =& new Post(array('title' => 'Post '.$i)); 
     415            $Post->tag->create(array('name' => 'Tag '.$i)); 
     416            $Post->save(); 
     417            $this->assertEqual($Post->tag->count(), 1); 
     418        } 
     419 
     420        $Post11 =& new Post(array('name' => 'Post 11')); 
     421        $this->assertTrue($Post11->save()); 
     422 
     423        $Post->tag->setByIds(1,2,3,4,5); 
     424         
     425        $this->assertTrue($Post =& $Post->find(10, array('include' => 'tags'))); 
     426 
     427        foreach (array_keys($Post->tags) as $k){ 
     428            $this->assertEqual($Post->tags[$k]->getId(), $k+1); 
     429        } 
     430         
     431        // Tag 10 should exist but unrelated to a post 
     432        $this->assertTrue($Tag =& $Post->tags[$k]->find(10)); 
     433        $this->assertEqual($Tag->post->count(), 0); 
     434 
     435        $Post11->tag->setByIds(array(10,1)); 
     436 
     437        $this->assertTrue($Tag =& $Tag->find(10, array('include'=>'posts'))); 
     438        $this->assertEqual($Tag->posts[0]->getId(), 11); 
     439 
     440 
     441    } 
     442    /**//** //**/ 
    406443} 
    407444 
    408 ak_test('test_AkActiveRecord_hasAndBelongsToMany_Associations', true); 
    409445 
    410446?> 
  • trunk/test/unit/lib/AkActiveRecord/AkHasMany.php

    r323 r328  
    11<?php 
    22 
    3 if(!defined('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION')){ 
    4     define('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION',false); 
    5 
    6  
    7 defined('AK_TEST_DATABASE_ON') ? null : define('AK_TEST_DATABASE_ON', true); 
    8 require_once(dirname(__FILE__).'/../../../fixtures/config/config.php'); 
    9  
    10  
    11 class test_AkActiveRecord_hasMany_Associations extends  AkUnitTest  
     3class HasManyTestCase extends AkUnitTest  
    124{ 
    13  
     5    /**/ 
    146    function test_start() 
    157    { 
     
    3527    } 
    3628 
    37     /**/ 
    3829    function test_for_has_many() 
    3930    { 
     
    213204    } 
    214205 
    215     /**/ 
    216  
    217206    function test_clean_up_dependencies() 
    218207    { 
     
    273262        $this->assertEqual($Result->comments[0]->get('name'), 'Aditya'); 
    274263    } 
    275     /**/ 
     264     
     265     
     266    function test_remove_existing_associates_before_setting_by_id() 
     267    { 
     268        $this->installAndIncludeModels(array('Post', 'Comment')); 
     269         
     270        foreach (range(1,10) as $i){ 
     271            $Post =& new Post(array('title' => 'Post '.$i)); 
     272            $Post->comment->create(array('name' => 'Comment '.$i)); 
     273            $Post->save(); 
     274        } 
     275                     
     276        $Post11 =& new Post(array('name' => 'Post 11')); 
     277        $this->assertTrue($Post11->save()); 
     278 
     279        $Post->comment->setByIds(1,2,3,4,5); 
     280        
     281        $this->assertTrue($Post =& $Post->find(10, array('include' => 'comments'))); 
     282         
     283         
     284        foreach (array_keys($Post->comments) as $k){ 
     285            $this->assertEqual($Post->comments[$k]->getId(), $k+1); 
     286        } 
     287         
     288        // Comment 10 should exist but unrelated to a post 
     289        $this->assertTrue($Comment =& $Post->comments[$k]->find(10)); 
     290        $this->assertNull($Comment->get('post_id')); 
     291                 
     292        $Post11->comment->setByIds(array(10,1)); 
     293         
     294        $this->assertTrue($Comment =& $Comment->find(10)); 
     295        $this->assertEqual($Comment->get('post_id'), 11); 
     296    } 
     297    /**//**//**/ 
    276298} 
    277299 
    278 ak_test('test_AkActiveRecord_hasMany_Associations', true); 
    279  
    280300 
    281301?>