Development

Changeset 11988 for plugins/sfModerationPlugin

You must first sign up to be able to contribute.

Show
Ignore:
Timestamp:
10/06/08 18:54:56 (5 years ago)
Author:
blacksun
Message:

added treatment of a class without comment column (refs #4558)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfModerationPlugin/lib/sfPropelModerationBehavior.class.php

    r11984 r11988  
    347347    * Retrieves the moderation comment of an object 
    348348    *  
    349     * @param  Mixed     Propel object 
    350     * 
    351     * @return Mixed     Moderation comment, or false if the column does not exist 
    352     */ 
    353   public static function getModerationComment($object) 
     349    * @param  BaseObject Propel object 
     350    * 
     351    * @return Mixed      Moderation comment, or false if the column does not exist 
     352    */ 
     353  public static function getModerationComment(BaseObject $object) 
     354  { 
     355    if (isset($object->_moderation_comment)) 
     356    { 
     357      return $object->_moderation_comment; 
     358    } 
     359    else 
     360    { 
     361      try 
     362      { 
     363        $class = get_class($object); 
     364        self::getColNameFromPeerClass('comment', $class.'Peer'); 
     365      } 
     366      catch (Exception $e) 
     367      { 
     368        return false; 
     369      } 
     370       
     371      return $object->getByName(self::getFieldNameFromClass('comment', $class), BasePeer::TYPE_FIELDNAME);       
     372    } 
     373  } 
     374   
     375  /** 
     376    * Sets the moderation comment of an object 
     377    *  
     378    * @param  BaseObject Propel object 
     379    * @param  String     Moderation comment 
     380    */ 
     381  public static function setModerationComment(BaseObject $object, $comment) 
    354382  { 
    355383    $class = get_class($object); 
    356      
     384    $object->_moderation_comment = $comment; 
    357385    try 
    358386    { 
    359387      self::getColNameFromPeerClass('comment', $class.'Peer'); 
     388      $object->setByName(self::getFieldNameFromClass('comment', $class), $comment, BasePeer::TYPE_FIELDNAME); 
    360389    } 
    361390    catch (Exception $e) 
     
    363392      return false; 
    364393    } 
    365      
    366     return $object->getByName(self::getFieldNameFromClass('comment', $class), BasePeer::TYPE_FIELDNAME); 
    367   } 
    368    
    369   /** 
    370     * Sets the moderation comment of an object 
    371     *  
    372     * @param  Mixed     Propel object 
    373     * @param  String    Moderation comment 
    374     */ 
    375   public static function setModerationComment($object, $comment) 
    376   { 
    377     $class = get_class($object); 
    378  
    379     try 
    380     { 
    381       self::getColNameFromPeerClass('comment', $class.'Peer'); 
    382     } 
    383     catch (Exception $e) 
    384     { 
    385       return false; 
    386     } 
    387      
    388     return $object->setByName(self::getFieldNameFromClass('comment', $class), $comment, BasePeer::TYPE_FIELDNAME); 
    389394  } 
    390395   
     
    469474   * Create a sf_moderated_content entry after saving a moderable object 
    470475   *  
    471    * @param  baseObject  $object 
     476   * @param  BaseObject  $object 
    472477   */ 
    473478  public function doPostSave(BaseObject $object) 
  • plugins/sfModerationPlugin/test/unit/sfPropelModerationBehaviorTest.php

    r11897 r11988  
    2121 */ 
    2222 
     23/* NB: If you want to avoid configuration, add the following table to your schema: 
     24moderation_test: 
     25  id: 
     26  title:       varchar(255) 
     27  content:     longvarchar 
     28  url:         longvarchar 
     29  extra:       varchar(255)  
     30  user_name:   varchar(255) 
     31  user_email:  varchar(50) 
     32  mod_status:  { type: integer, default: 1, index: true } 
     33  mod_comment: varchar(255) 
     34  updated_at: 
     35*/ 
     36 
    2337// configuration 
    24 // Autofind the first available app environment 
    2538$sf_root_dir = realpath(dirname(__FILE__).'/../../../../'); 
    2639$apps_dir = glob($sf_root_dir.'/apps/*', GLOB_ONLYDIR); 
     
    3649$sf_path = dirname(__FILE__).'/../../../..'; 
    3750  
    38 // bootstrap 
     51// functional bootstrap 
    3952include($sf_path . '/test/bootstrap/functional.php'); 
    40  
    41 // -- the model class and columns the tests should use 
    42 /* If you want to avoid configuration, add the following table (the same for all unit tests) to your schema: 
    43 moderation_test: 
    44   id: 
    45   title:       varchar(255) 
    46   content:     longvarchar 
    47   url:         longvarchar 
    48   extra:       varchar(255)  
    49   user_name:   varchar(255) 
    50   user_email:  varchar(50) 
    51   mod_status:  { type: integer, default: 1, index: true } 
    52   mod_comment: varchar(255) 
    53   updated_at: 
    54 */ 
    55 $test_class          = sfConfig::get('app_sfModerationPlugin_test_class',       'ModerationTest'); 
    56 $test_status_column  = sfConfig::get('app_sfModerationPlugin_test_status_column',  'mod_status'); 
    57 $test_comment_column = sfConfig::get('app_sfModerationPlugin_test_comment_column', 'mod_comment'); 
    58 $test_title_column   = sfConfig::get('app_sfModerationPlugin_test_title_column',   'Title'); 
    59 $test_content_column = sfConfig::get('app_sfModerationPlugin_test_content_column', 'Content'); 
    60 $test_url_column = sfConfig::get('app_sfModerationPlugin_test_url_column', 'Url'); 
    61 $test_username_column = sfConfig::get('app_sfModerationPlugin_test_username_column', 'UserName'); 
    62 $test_useremail_column = sfConfig::get('app_sfModerationPlugin_test_useremail_column', 'UserEmail'); 
    63 $test_watch_columns = sfConfig::get('app_sfModerationPlugin_test_watch_columns', array( 'title', 'content', 'user_name', 'url')); 
    64 // create a new test browser 
    65 $browser = new sfTestBrowser(); 
    66 $browser->initialize(); 
    67  
    68 // initialize database manager 
    69 $databaseManager = new sfDatabaseManager(); 
    70 $databaseManager->initialize(); 
    71  
    72 $con = Propel::getConnection(); 
    73  
    74 $test_peer_class = $test_class.'Peer'; 
    75  
    76 // cleanup database 
    77 call_user_func(array($test_peer_class, 'doDeleteAll')); 
    78  
    79 // register behavior on test object 
    80 sfPropelBehavior::add($test_class, array(sfPropelModerationBehavior::BEHAVIOR_NAME => array( 
    81   'status_column'      => $test_status_column, 
    82   'comment_column'     => $test_comment_column, 
    83   'title_getter'       => 'get'.$test_title_column, 
    84   'content_getter'     => 'get'.$test_content_column, 
    85   'url_getter'         => 'get'.$test_url_column, 
    86   'username_getter'    => 'get'.$test_username_column, 
    87   'useremail_getter'   => 'get'.$test_useremail_column, 
    88   'watch_columns'      => $test_watch_columns, 
    89 ))); 
     53require_once(sfConfig::get('sf_symfony_lib_dir').'/vendor/lime/lime.php');  
    9054 
    9155 
    9256// Now we can start to test 
    93 $t = new lime_test(61, new lime_output_color()); 
     57$h = new lime_harness(new lime_output_color()); 
     58$h->base_dir = dirname(__FILE__) . '/all'; 
    9459 
    95 $t->diag('new methods'); 
    96 $methods = array( 
    97   'tagAsUnsafe', 
    98   'tagAsSafe', 
    99   'getModerationStatus', 
    100   'setModerationStatus', 
    101   'getModerationStatusString', 
    102   'setModerationStatusString', 
    103   'getModerationComment', 
    104   'setModerationComment', 
    105   'getModeratedContentFromObject' 
    106 ); 
    107 foreach ($methods as $method) 
    108 
    109   $t->ok(is_callable($test_class, $method), sprintf('Behavior adds a new %s() method to the object class', $method)); 
    110 
     60// register all tests 
     61$finder = sfFinder::type('file')->ignore_version_control()->follow_link()->name('*Test.php'); 
     62$h->register($finder->in($h->base_dir)); 
    11163 
    112 $t->diag('getModerationStatus(), setModerationStatus(), getModerationComment(), setModerationComment()'); 
    113 $item1 = new $test_class(); 
    114 // Object must be modified to be saved 
    115 $item1->setByName($test_title_column, 'foo'); 
    116 $item1->save(); 
    117 $t->is($item1->getModerationStatus(), 1, 'getModerationStatus() returns the current moderation status (defaults to 1 = not checked)'); 
    118 $item1->setModerationStatus(0); 
    119 $t->is($item1->getModerationStatus(), 0, 'setModerationStatus() changes the moderation status'); 
    120 $t->diag('getModerationComment() and setModerationComment()'); 
    121 $t->is($item1->getModerationComment(), '', 'getModerationComment() returns the current moderation comment (defaults to empty string)'); 
    122 $t->is($item1->setModerationComment('Moderated because foo bar'), null, 'setModerationComment() returns null if the moderation comment column exists'); 
    123 $t->is($item1->getModerationComment(), 'Moderated because foo bar', 'getModerationComment() returns the current moderation comment'); 
    124  
    125  
    126 $t->diag('modifications of selection and count methods'); 
    127 call_user_func(array($test_peer_class, 'doDeleteAll')); 
    128 $item1 = new $test_class(); 
    129 $item1->setModerationStatus(sfPropelModerationBehavior::NOT_CHECKED); 
    130 $item1->save(); 
    131 $id1 = $item1->getId(); 
    132 $item2 = new $test_class(); 
    133 $item2->setModerationStatus(sfPropelModerationBehavior::TAGGED_UNSAFE); 
    134 $item2->save(); 
    135 $id2 = $item2->getId(); 
    136 $item3 = new $test_class(); 
    137 $item3->setModerationStatus(sfPropelModerationBehavior::TAGGED_SAFE); 
    138 $item3->save(); 
    139 $id3 = $item3->getId(); 
    140 $t->isa_ok(call_user_func(array($test_peer_class, 'retrieveByPk'), $id1), $test_class, 'retrieveByPk() finds unmarked records'); 
    141 $t->isa_ok(call_user_func(array($test_peer_class, 'retrieveByPk'), $id2), 'NULL', 'retrieveByPk() doesn\'t find records marked as spam'); 
    142 $t->isa_ok(call_user_func(array($test_peer_class, 'retrieveByPk'), $id3), $test_class, 'retrieveByPk() finds records marked as safe'); 
    143 $t->is(call_user_func(array($test_peer_class, 'doCount'), new Criteria()), 2, 'doCount() ignores records marked as spam'); 
    144 $t->is(count(call_user_func(array($test_peer_class, 'doSelect'), new Criteria())), 2, 'doSelect() ignores records marked as spam'); 
    145  
    146 $t->diag('Publish/unpublish messages by default via app.yml'); 
    147 sfConfig::set('app_sfModerationPlugin_display_treshold', sfPropelModerationBehavior::TAGGED_SAFE); 
    148 $t->isa_ok(call_user_func(array($test_peer_class, 'retrieveByPk'), $id1), 'NULL', 'Setting `app_sfModerationPlugin_display_treshold` to 0 ignores unchecked records from normal selection'); 
    149 $t->is(call_user_func(array($test_peer_class, 'doCount'), new Criteria()), 1, 'Setting `app_sfModerationPlugin_display_treshold` to 0 ignores unchecked records from normal selection'); 
    150 sfConfig::set('app_sfModerationPlugin_display_treshold', sfPropelModerationBehavior::NOT_CHECKED); 
    151 $t->isa_ok(call_user_func(array($test_peer_class, 'retrieveByPk'), $id1), $test_class, 'Setting `app_sfModerationPlugin_display_treshold` to 1 revelas unchecked records from normal selection'); 
    152 $t->is(call_user_func(array($test_peer_class, 'doCount'), new Criteria()), 2, 'Setting `app_sfModerationPlugin_display_treshold` to 1 reveals unchecked records from normal selection'); 
    153 sfConfig::set('app_sfModerationPlugin_display_treshold', sfPropelModerationBehavior::TAGGED_UNSAFE); 
    154 $t->isa_ok(call_user_func(array($test_peer_class, 'retrieveByPk'), $id1), $test_class, 'Setting `app_sfModerationPlugin_display_treshold` to 3 lets all records through'); 
    155 $t->is(call_user_func(array($test_peer_class, 'doCount'), new Criteria()), 3, 'Setting `app_sfModerationPlugin_display_treshold` to 3 lets all records through'); 
    156 sfConfig::set('app_sfModerationPlugin_display_treshold', sfPropelModerationBehavior::NOT_CHECKED); 
    157  
    158 $t->diag('tagAsUnsafe() and tagAsSafe()'); 
    159 call_user_func(array($test_peer_class, 'doDeleteAll')); 
    160 $item1 = new $test_class(); 
    161 // Object must be modified to be saved 
    162 $item1->setByName($test_title_column, 'foo'); 
    163 $item1->save(); 
    164 $id1 = $item1->getId(); 
    165 $item1->tagAsUnsafe(); 
    166 $t->isa_ok(call_user_func(array($test_peer_class, 'retrieveByPk'), $id1), 'NULL', 'tagAsUnsafe() changes the status to spam and saves the record'); 
    167 $t->is($item1->getModerationStatus(), sfPropelModerationBehavior::TAGGED_UNSAFE, 'tagAsUnsafe() changes the status to spam and saves the record'); 
    168 $item1->tagAsUnsafe(sfPropelModerationBehavior::AUTO_TAGGED_UNSAFE); 
    169 $t->is($item1->getModerationStatus(), sfPropelModerationBehavior::AUTO_TAGGED_UNSAFE, 'tagAsUnsafe(sfPropelModerationBehavior::AUTO_TAGGED_UNSAFE) changes the status to auto_spam and saves the record'); 
    170 $item1->tagAsUnsafe(12); 
    171 $t->is($item1->getModerationStatus(), 12, 'tagAsUnsafe() accepts custom status codes'); 
    172 $item2 = new $test_class(); 
    173 // Object must be modified to be saved 
    174 $item2->setByName($test_title_column, 'foo'); 
    175 $item2->save(); 
    176 $id2 = $item2->getId(); 
    177 $item2->tagAsSafe(); 
    178 $t->isa_ok(call_user_func(array($test_peer_class, 'retrieveByPk'), $id2), $test_class, 'tagAsSafe() changes the status to safe and saves the record'); 
    179 $t->is($item2->getModerationStatus(), sfPropelModerationBehavior::TAGGED_SAFE, 'tagAsSafe() changes the status to safe and saves the record'); 
    180  
    181 $t->diag('sfPropelModerationBehavior::enable() and disable()'); 
    182 call_user_func(array($test_peer_class, 'doDeleteAll')); 
    183 $item1 = new $test_class(); 
    184 $item1->setModerationStatus(sfPropelModerationBehavior::NOT_CHECKED); 
    185 $item1->save(); 
    186 $item2 = new $test_class(); 
    187 $item2->setModerationStatus(sfPropelModerationBehavior::TAGGED_UNSAFE); 
    188 $item2->save(); 
    189 $item3 = new $test_class(); 
    190 $item3->setModerationStatus(sfPropelModerationBehavior::TAGGED_SAFE); 
    191 $item3->save(); 
    192 $t->is(call_user_func(array($test_peer_class, 'doCount'), new Criteria()), 2, 'Spam check is enabled by default in selections'); 
    193 sfPropelModerationBehavior::disable(); 
    194 $t->is(call_user_func(array($test_peer_class, 'doCount'), new Criteria()), 3, 'Setting sfPropelModerationBehavior::disable() disables the spam check in selections'); 
    195 sfPropelModerationBehavior::enable(); 
    196 $t->is(call_user_func(array($test_peer_class, 'doCount'), new Criteria()), 2, 'Setting sfPropelModerationBehavior::enable() enables the spam check in selections'); 
    197  
    198  
    199 //$t->is($item1->); 
    200  
    201 $t->diag('Monitoring and sfModeratedContent'); 
    202 sfConfig::set('app_sfModerationPlugin_monitoring_treshold', sfPropelModerationBehavior::TAGGED_SAFE); 
    203 $item1 = new $test_class(); 
    204 $item1->setByName($test_title_column, 'foo'); 
    205 $item1->setByName($test_content_column, 'bar'); 
    206 $item1->setByName($test_url_column, 'http://foo'); 
    207 $item1->setByName($test_username_column, 'mister bar'); 
    208 $item1->setByName($test_useremail_column, 'foo@bar.com'); 
    209 $item1->save(); 
    210 $t->is($item1->getModeratedContentFromObject(), null, 'monitoring is not enabled by default and getModeratedContentFromObject() returns null'); 
    211 sfConfig::set('app_sfModerationPlugin_is_monitored', true); 
    212 // Object must be modified to be saved 
    213 $item1->setByName($test_title_column, 'bar'); 
    214 $item1->save(); 
    215 $t->isa_ok($item1->getModeratedContentFromObject(), 'sfModeratedContent', 'per class monitoring is enabled by default'); 
    216 sfConfig::set('propel_behavior_moderation_'.$test_class.'_is_monitored', false); 
    217 $t->is($item1->getModeratedContentFromObject(), null, 'monitoring can be disabled on a per class basis and getModeratedContentFromObject() returns null'); 
    218 sfConfig::set('propel_behavior_moderation_'.$test_class.'_is_monitored', true); 
    219 // Object must be modified to be saved 
    220 $item1->setByName($test_title_column, 'foo'); 
    221 $item1->save(); 
    222 $moderated_content = $item1->getModeratedContentFromObject(); 
    223 $t->isa_ok($moderated_content, 'sfModeratedContent', 'enabling monitoring both globally and on an object activates auto creation of an sfModeratedContent object each time a monitored object is saved'); 
    224 $t->is($moderated_content->getObjectId(), $item1->getId(), 'sfModeratedContent object gets the Propel object id'); 
    225 $t->is($moderated_content->getObjectModel(), $test_class, 'sfModeratedContent object gets the Propel object class'); 
    226 $t->is($moderated_content->getTitle(), 'foo', 'sfModeratedContent object gets the Propel object title'); 
    227 $t->is($moderated_content->getContent(), 'bar', 'sfModeratedContent object gets the Propel object content'); 
    228 $t->is($moderated_content->getUrl(), 'http://foo', 'sfModeratedContent object gets the Propel object url'); 
    229 $t->is($moderated_content->getUsername(), 'mister bar', 'sfModeratedContent object gets the Propel object user name'); 
    230 $t->is($moderated_content->getUserEmail(), 'foo@bar.com', 'sfModeratedContent object gets the Propel object user email'); 
    231 $item1->setModerationComment('This is foo'); 
    232 $item1->tagAsUnsafe(); 
    233 $moderated_content = $item1->getModeratedContentFromObject(); 
    234 $t->is($moderated_content->getStatus(), sfPropelModerationBehavior::TAGGED_UNSAFE, 'sfModeratedContent object gets the Propel object moderation status'); 
    235 $t->is($moderated_content->getComment(), 'This is foo', 'sfModeratedContent object gets the Propel object moderation comment'); 
    236 $moderated_content_id = $moderated_content->getId(); 
    237 $item1->save(); 
    238 $moderated_content = $item1->getModeratedContentFromObject(); 
    239 $t->is($moderated_content->getId(), $moderated_content_id, 'updating a moderated object updates the related sfModeratedContent and does not create a new one'); 
    240 $item1->delete(); 
    241 $moderated_content = $item1->getModeratedContentFromObject(); 
    242 $t->is($item1->getModeratedContentFromObject(), null, 'deleting a monitored object also deletes its related sfModeratedContent'); 
    243  
    244 $t->diag('Watched columns'); 
    245 $watch_columns = sfPropelModerationBehavior::getWatchedColumns($test_class); 
    246 $common_columns = array_intersect($watch_columns, $test_watch_columns); 
    247 $t->is(count($common_columns), count($test_watch_columns), 'getWatchedColumns() includes all config watched columns'); 
    248 $t->ok(in_array($test_status_column, $watch_columns), 'getWatchedColumns() includes config watched columns'); 
    249 $t->ok(in_array($test_comment_column, $watch_columns), 'getWatchedColumns() includes config watched columns'); 
    250 if (constant($test_peer_class.'::EXTRA')) 
    251 
    252   // We have extra non-moderated column 
    253   sfConfig::set('app_sfModerationPlugin_is_monitored', true); 
    254   sfConfig::set('propel_behavior_moderation_'.$test_class.'_is_monitored', true); 
    255   $test_extra_column = 'Extra'; 
    256   $item1 = new $test_class(); 
    257   $item1->setByName($test_extra_column, 'foo'); 
    258   $item1->save(); 
    259   $t->is($item1->getModeratedContentFromObject(), null, 'Modifying non-watched columns doesn\'t trigger moderation and getModeratedContentFromObject() returns null'); 
    260 
    261  
    262 $t->diag('sfModeratedContent dates'); 
    263 $item1 = new $test_class(); 
    264 // Object must be modified to be saved 
    265 $item1->setByName($test_title_column, 'foo'); 
    266 $item1->save(); 
    267 $item1_updated_at = $item1->getUpdatedAt('U'); 
    268 $moderated_content = $item1->getModeratedContentFromObject(); 
    269 $t->is($moderated_content->getObjectUpdatedAt('U'), $item1_updated_at, 'sfModeratedContent object gets the Propel object update date in object_updated_at'); 
    270 $t->is($moderated_content->getModeratedAt('U'), $item1_updated_at, 'sfModeratedContent knows when it was last moderated in moderated_at'); 
    271 sleep(1); 
    272 $item1->tagAsSafe(); 
    273 $moderated_content = $item1->getModeratedContentFromObject(); 
    274 $t->is($moderated_content->getObjectUpdatedAt('U'), $item1_updated_at, 'Tagging a propel object doesn\'t change its update date'); 
    275 $t->isnt($moderated_content->getModeratedAt('U'), $item1_updated_at, 'Tagging a propel object changes the related sfModeratedContent moderated_at date'); 
    276  
    277 $t->diag('Monitoring treshold'); 
    278 sfConfig::set('app_sfModerationPlugin_monitoring_treshold', sfPropelModerationBehavior::AUTO_TAGGED_UNSAFE); 
    279 $item1 = new $test_class(); 
    280 // Object must be modified to be saved 
    281 $item1->setByName($test_title_column, 'foo'); 
    282 $item1->save(); 
    283 $moderated_content = $item1->getModeratedContentFromObject(); 
    284 $t->isa_ok($moderated_content, 'NULL', 'Saving an object with a moderation status below the treshold doesn\'t save a sfModeratedContent'); 
    285 $item1->tagAsUnsafe(); 
    286 $moderated_content = $item1->getModeratedContentFromObject(); 
    287 $t->isa_ok($moderated_content, 'sfModeratedContent', 'Saving an object with a moderation status greater than the treshold saves a sfModeratedContent'); 
    288 $item1->tagAsSafe(); 
    289 $moderated_content = $item1->getModeratedContentFromObject(); 
    290 $t->isa_ok($moderated_content, 'NULL', 'Declaring an object as safe removes the related sfModeratedContent with the default monitoring treshold'); 
    291  
    292 $t->diag('Utility methods'); 
    293 class myClass 
    294 
    295   public function foo2() 
    296   { 
    297     return 'bar'; 
    298   } 
    299   public function foo3() 
    300   { 
    301     return 'bar3'; 
    302   } 
    303  
    304 
    305 $myobject = new myClass(); 
    306 $t->is(sfModeratedContentPeer::getOneOf($myobject, array('foo0', 'foo1', 'foo2', 'foo3')), 'bar', 'sfModeratedContentPeer::getOneOf() tests several methods on an object and returns the result of the first matching one'); 
     64$h->run();