Skip to content

Commit

Permalink
fixes #6209, persistModel and undefined $Behaviors
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8117 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information
gwoo committed Mar 19, 2009
1 parent 3c3b852 commit 5c53fcf
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 6 deletions.
16 changes: 11 additions & 5 deletions cake/libs/object.php
Expand Up @@ -248,7 +248,11 @@ function _savePersistent($name, &$object) {
$objectArray = array(&$object);
$data = str_replace('\\', '\\\\', serialize($objectArray));
$data = '<?php $' . $name . ' = \'' . str_replace('\'', '\\\'', $data) . '\' ?>';
cache($file, $data, '+1 day');
$duration = '+999 days';
if (Configure::read() >= 1) {
$duration = '+10 seconds';
}
cache($file, $data, $duration);
}
/**
* Open the persistent class file for reading
Expand All @@ -267,14 +271,16 @@ function __openPersistent($name, $type = null) {
case 'registry':
$vars = unserialize(${$name});
foreach ($vars['0'] as $key => $value) {
App::import('Model', Inflector::classify($key));
if (strpos($key, '_behavior') !== false) {
App::import('Behavior', Inflector::classify(substr($key, 0, -9)));
} else {
App::import('Model', Inflector::classify($key));
}
unset ($value);
}
unset($vars);
$vars = unserialize(${$name});
foreach ($vars['0'] as $key => $value) {
foreach ($vars['0'][$key]->Behaviors->_attached as $behavior) {
App::import('Behavior', $behavior);
}
ClassRegistry::addObject($key, $value);
unset ($value);
}
Expand Down
131 changes: 130 additions & 1 deletion cake/tests/cases/libs/object.test.php
Expand Up @@ -117,6 +117,37 @@ function params_pass() {
return $this->params;
}
}
/**
* RequestActionPersistentController class
*
* @package cake
* @subpackage cake.tests.cases.libs
*/
class RequestActionPersistentController extends Controller {
/**
* uses property
*
* @var array
* @access public
*/
var $uses = array('PersisterOne');

/**
* persistModel property
*
* @var array
* @access public
*/
var $persistModel = true;
/**
* post pass, testing post passing
*
* @return array
**/
function index() {
return 'This is a test';
}
}
/**
* TestObject class
*
Expand Down Expand Up @@ -271,7 +302,7 @@ class ObjectTest extends CakeTestCase {
*
* @var string
**/
var $fixtures = array('core.post');
var $fixtures = array('core.post', 'core.comment');
/**
* setUp method
*
Expand Down Expand Up @@ -382,6 +413,104 @@ function testPersist() {
Configure::write('Cache.disable', $cacheDisable);
}
/**
* testPersistWithRequestAction method
*
* @access public
* @return void
*/
function testPersistWithBehavior() {
ClassRegistry::flush();

$cacheDisable = Configure::read('Cache.disable');
Configure::write('Cache.disable', false);

Configure::write('modelPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models'. DS));
Configure::write('behaviorPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models'. DS . 'behaviors' . DS));

$this->assertFalse(class_exists('PersisterOneBehaviorBehavior'));
$this->assertFalse(class_exists('PersisterTwoBehaviorBehavior'));

$Controller = new RequestActionPersistentController();
$Controller->persistModel = true;
$Controller->constructClasses();

$this->assertTrue(file_exists(CACHE . 'persistent' . DS . 'persisterone.php'));
$this->assertTrue(file_exists(CACHE . 'persistent' . DS . 'persisteroneregistry.php'));

$contents = str_replace('"PersisterOne"', '"PersisterTwo"', file_get_contents(CACHE . 'persistent' . DS . 'persisteroneregistry.php'));
$contents = str_replace('persister_one_', 'persister_two_', file_get_contents(CACHE . 'persistent' . DS . 'persisteroneregistry.php'));

$result = file_put_contents(CACHE . 'persistent' . DS . 'persisteroneregistry.php', $contents);

$this->assertTrue(class_exists('PersisterOneBehaviorBehavior'));
$this->assertFalse(class_exists('PersisterTwoBehaviorBehavior'));

$Controller = new RequestActionPersistentController();
$Controller->persistModel = true;
$Controller->constructClasses();

$this->assertTrue(class_exists('PersisterOneBehaviorBehavior'));
$this->assertTrue(class_exists('PersisterTwoBehaviorBehavior'));

@unlink(CACHE . 'persistent' . DS . 'persisterone.php');
@unlink(CACHE . 'persistent' . DS . 'persisteroneregistry.php');
}
/**
* testPersistWithBehaviorAndRequestAction method
*
* @see testPersistWithBehavior
* @access public
* @return void
*/
function testPersistWithBehaviorAndRequestAction() {
ClassRegistry::flush();

$cacheDisable = Configure::read('Cache.disable');
Configure::write('Cache.disable', false);

$this->assertFalse(class_exists('ContainableBehavior'));

Configure::write('modelPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models'. DS));
Configure::write('behaviorPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models'. DS . 'behaviors' . DS));

$this->assertFalse(class_exists('PersistOneBehaviorBehavior'));
$this->assertFalse(class_exists('PersistTwoBehaviorBehavior'));

$Controller = new RequestActionPersistentController();
$Controller->persistModel = true;
$Controller->constructClasses();

$this->assertTrue(file_exists(CACHE . 'persistent' . DS . 'persisterone.php'));
$this->assertTrue(file_exists(CACHE . 'persistent' . DS . 'persisteroneregistry.php'));

$keys = ClassRegistry::keys();
$this->assertEqual($keys, array('persister_one', 'comment', 'persister_one_behavior_behavior'));

ob_start();
$Controller->set('content_for_layout', 'cool');
$Controller->render('index', 'ajax', '/layouts/ajax');
$result = ob_get_clean();

$keys = ClassRegistry::keys();
$this->assertEqual($keys, array('persister_one', 'comment', 'persister_one_behavior_behavior', 'view'));

$result = $this->object->requestAction('/request_action_persistent/index');
$expected = 'This is a test';
$this->assertEqual($result, $expected);

@unlink(CACHE . 'persistent' . DS . 'persisterone.php');
@unlink(CACHE . 'persistent' . DS . 'persisteroneregistry.php');

$Controller = new RequestActionPersistentController();
$Controller->persistModel = true;
$Controller->constructClasses();

@unlink(CACHE . 'persistent' . DS . 'persisterone.php');
@unlink(CACHE . 'persistent' . DS . 'persisteroneregistry.php');

Configure::write('Cache.disable', $cacheDisable);
}
/**
* testToString method
*
* @access public
Expand Down

0 comments on commit 5c53fcf

Please sign in to comment.