diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 422b8697644..aeebf3ea0cf 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -1629,7 +1629,7 @@ public function save($data = null, $validate = true, $fieldList = array()) { if (!array_key_exists('format', $colType)) { $time = strtotime('now'); } else { - $time = $colType['formatter']($colType['format']); + $time = call_user_func($colType['formatter'], $colType['format']); } if (!empty($this->whitelist)) { $this->whitelist[] = $updateCol; diff --git a/lib/Cake/Test/Case/Controller/Component/Auth/FormAuthenticateTest.php b/lib/Cake/Test/Case/Controller/Component/Auth/FormAuthenticateTest.php index 09097d4b7de..a65e04f285a 100644 --- a/lib/Cake/Test/Case/Controller/Component/Auth/FormAuthenticateTest.php +++ b/lib/Cake/Test/Case/Controller/Component/Auth/FormAuthenticateTest.php @@ -163,7 +163,6 @@ public function testPluginModel() { ), App::RESET); CakePlugin::load('TestPlugin'); - $ts = date('Y-m-d H:i:s'); $PluginModel = ClassRegistry::init('TestPlugin.TestPluginAuthUser'); $user['id'] = 1; $user['username'] = 'gwoo'; @@ -185,7 +184,7 @@ public function testPluginModel() { 'username' => 'gwoo', 'created' => '2007-03-17 01:16:23' ); - $this->assertTrue($result['updated'] >= $ts); + $this->assertEquals(self::date(), $result['updated']); unset($result['updated']); $this->assertEquals($expected, $result); CakePlugin::unload(); diff --git a/lib/Cake/Test/Case/Model/ModelIntegrationTest.php b/lib/Cake/Test/Case/Model/ModelIntegrationTest.php index 05afa857a77..180e1aeac66 100644 --- a/lib/Cake/Test/Case/Model/ModelIntegrationTest.php +++ b/lib/Cake/Test/Case/Model/ModelIntegrationTest.php @@ -1867,7 +1867,6 @@ public function testWithAssociation() { 'doomed' => true )))); - $ts = date('Y-m-d H:i:s'); $TestModel->save(); $TestModel->hasAndBelongsToMany['SomethingElse']['order'] = 'SomethingElse.id ASC'; @@ -1921,7 +1920,7 @@ public function testWithAssociation() { ) ) ); - $this->assertTrue($result['Something']['updated'] >= $ts); + $this->assertEquals(self::date(), $result['Something']['updated']); unset($result['Something']['updated']); $this->assertEquals($expected, $result); } diff --git a/lib/Cake/Test/Case/Model/ModelWriteTest.php b/lib/Cake/Test/Case/Model/ModelWriteTest.php index c06c964d921..ff51b5f6ff0 100644 --- a/lib/Cake/Test/Case/Model/ModelWriteTest.php +++ b/lib/Cake/Test/Case/Model/ModelWriteTest.php @@ -1613,7 +1613,6 @@ public function testSaveHabtmNoPrimaryData() { ); $this->assertEquals($expected, $result); - $ts = date('Y-m-d H:i:s'); $TestModel->id = 2; $data = array('Tag' => array('Tag' => array(2))); $TestModel->save($data); @@ -1627,7 +1626,7 @@ public function testSaveHabtmNoPrimaryData() { 'body' => 'Second Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', - 'updated' => $ts + 'updated' => self::date() ), 'Tag' => array( array( @@ -2562,29 +2561,28 @@ public function testSaveMultipleHabtm() { $this->assertEquals($expected, $result); - $ts = date('Y-m-d H:i:s'); $TestModel->id = 1; $data = array( 'JoinA' => array( 'id' => '1', 'name' => 'New name for Join A 1', - 'updated' => $ts + 'updated' => self::date() ), 'JoinB' => array( array( 'id' => 1, 'join_b_id' => 2, 'other' => 'New data for Join A 1 Join B 2', - 'created' => $ts, - 'updated' => $ts + 'created' => self::date(), + 'updated' => self::date() )), 'JoinC' => array( array( 'id' => 1, 'join_c_id' => 2, 'other' => 'New data for Join A 1 Join C 2', - 'created' => $ts, - 'updated' => $ts + 'created' => self::date(), + 'updated' => self::date() ))); $TestModel->set($data); @@ -2597,7 +2595,7 @@ public function testSaveMultipleHabtm() { 'name' => 'New name for Join A 1', 'body' => 'Join A 1 Body', 'created' => '2008-01-03 10:54:23', - 'updated' => $ts + 'updated' => self::date() ), 'JoinB' => array( 0 => array( @@ -2610,8 +2608,8 @@ public function testSaveMultipleHabtm() { 'join_a_id' => 1, 'join_b_id' => 2, 'other' => 'New data for Join A 1 Join B 2', - 'created' => $ts, - 'updated' => $ts + 'created' => self::date(), + 'updated' => self::date() ))), 'JoinC' => array( 0 => array( @@ -2624,8 +2622,8 @@ public function testSaveMultipleHabtm() { 'join_a_id' => 1, 'join_c_id' => 2, 'other' => 'New data for Join A 1 Join C 2', - 'created' => $ts, - 'updated' => $ts + 'created' => self::date(), + 'updated' => self::date() )))); $this->assertEquals($expected, $result); @@ -2643,7 +2641,6 @@ public function testSaveAll() { $result = $TestModel->find('all'); $this->assertEquals(3, count($result)); $this->assertFalse(isset($result[3])); - $ts = date('Y-m-d H:i:s'); $TestModel->saveAll(array( 'Post' => array( @@ -2670,10 +2667,10 @@ public function testSaveAll() { 'password' => '5f4dcc3b5aa765d61d8327deb882cf90', 'test' => 'working' )); - $this->assertTrue($result[3]['Post']['created'] >= $ts); - $this->assertTrue($result[3]['Post']['updated'] >= $ts); - $this->assertTrue($result[3]['Author']['created'] >= $ts); - $this->assertTrue($result[3]['Author']['updated'] >= $ts); + $this->assertEquals(self::date(), $result[3]['Post']['created']); + $this->assertEquals(self::date(), $result[3]['Post']['updated']); + $this->assertEquals(self::date(), $result[3]['Author']['created']); + $this->assertEquals(self::date(), $result[3]['Author']['updated']); unset($result[3]['Post']['created'], $result[3]['Post']['updated']); unset($result[3]['Author']['created'], $result[3]['Author']['updated']); $this->assertEquals($expected, $result[3]); @@ -2685,7 +2682,6 @@ public function testSaveAll() { // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass $this->db->truncate($TestModel); - $ts = date('Y-m-d H:i:s'); $TestModel->saveAll(array( array( 'title' => 'Multi-record post 1', @@ -2719,16 +2715,15 @@ public function testSaveAll() { 'body' => 'Second multi-record post', 'published' => 'N' ))); - $this->assertTrue($result[0]['Post']['created'] >= $ts); - $this->assertTrue($result[0]['Post']['updated'] >= $ts); - $this->assertTrue($result[1]['Post']['created'] >= $ts); - $this->assertTrue($result[1]['Post']['updated'] >= $ts); + $this->assertEquals(self::date(), $result[0]['Post']['created']); + $this->assertEquals(self::date(), $result[0]['Post']['updated']); + $this->assertEquals(self::date(), $result[1]['Post']['created']); + $this->assertEquals(self::date(), $result[1]['Post']['updated']); unset($result[0]['Post']['created'], $result[0]['Post']['updated']); unset($result[1]['Post']['created'], $result[1]['Post']['updated']); $this->assertEquals($expected, $result); $TestModel = new Comment(); - $ts = date('Y-m-d H:i:s'); $result = $TestModel->saveAll(array( 'Comment' => array( 'article_id' => 2, @@ -2749,8 +2744,8 @@ public function testSaveAll() { 'comment' => 'New comment with attachment', 'published' => 'Y' ); - $this->assertTrue($result[6]['Comment']['created'] >= $ts); - $this->assertTrue($result[6]['Comment']['updated'] >= $ts); + $this->assertEquals(self::date(), $result[6]['Comment']['created']); + $this->assertEquals(self::date(), $result[6]['Comment']['updated']); unset($result[6]['Comment']['created'], $result[6]['Comment']['updated']); $this->assertEquals($expected, $result[6]['Comment']); @@ -2759,8 +2754,8 @@ public function testSaveAll() { 'comment_id' => '7', 'attachment' => 'some_file.tgz' ); - $this->assertTrue($result[6]['Attachment']['created'] >= $ts); - $this->assertTrue($result[6]['Attachment']['updated'] >= $ts); + $this->assertEquals(self::date(), $result[6]['Attachment']['created']); + $this->assertEquals(self::date(), $result[6]['Attachment']['updated']); unset($result[6]['Attachment']['created'], $result[6]['Attachment']['updated']); $this->assertEquals($expected, $result[6]['Attachment']); } @@ -4057,7 +4052,6 @@ public function testSaveAllTransaction() { array('author_id' => 1, 'title' => 'New Fifth Post'), array('author_id' => 1, 'title' => '') ); - $ts = date('Y-m-d H:i:s'); $this->assertFalse($TestModel->saveAll($data)); $result = $TestModel->find('all', array('recursive' => -1)); @@ -4099,8 +4093,8 @@ public function testSaveAllTransaction() { 'title' => 'New Fourth Post', 'body' => null, 'published' => 'N', - 'created' => $ts, - 'updated' => $ts + 'created' => self::date(), + 'updated' => self::date() )); $expected[] = array( @@ -4110,8 +4104,8 @@ public function testSaveAllTransaction() { 'title' => 'New Fifth Post', 'body' => null, 'published' => 'N', - 'created' => $ts, - 'updated' => $ts + 'created' => self::date(), + 'updated' => self::date() )); $this->assertEquals($expected, $result); @@ -4126,7 +4120,6 @@ public function testSaveAllTransaction() { array('author_id' => 1, 'title' => ''), array('author_id' => 1, 'title' => 'New Sixth Post') ); - $ts = date('Y-m-d H:i:s'); $this->assertFalse($TestModel->saveAll($data)); $result = $TestModel->find('all', array('recursive' => -1)); @@ -4168,8 +4161,8 @@ public function testSaveAllTransaction() { 'title' => 'New Fourth Post', 'body' => 'Third Post Body', 'published' => 'N', - 'created' => $ts, - 'updated' => $ts + 'created' => self::date(), + 'updated' => self::date() )); $expected[] = array( @@ -4179,8 +4172,8 @@ public function testSaveAllTransaction() { 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'N', - 'created' => $ts, - 'updated' => $ts + 'created' => self::date(), + 'updated' => self::date() )); } $this->assertEquals($expected, $result); @@ -4265,7 +4258,6 @@ public function testSaveAllValidation() { 'author_id' => 2 )); - $ts = date('Y-m-d H:i:s'); $this->assertTrue($TestModel->saveAll($data)); $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC')); @@ -4306,10 +4298,10 @@ public function testSaveAllValidation() { 'body' => 'Fourth post body', 'published' => 'N' ))); - $this->assertTrue($result[0]['Post']['updated'] >= $ts); - $this->assertTrue($result[1]['Post']['updated'] >= $ts); - $this->assertTrue($result[3]['Post']['created'] >= $ts); - $this->assertTrue($result[3]['Post']['updated'] >= $ts); + $this->assertEquals(self::date(), $result[0]['Post']['updated']); + $this->assertEquals(self::date(), $result[1]['Post']['updated']); + $this->assertEquals(self::date(), $result[3]['Post']['created']); + $this->assertEquals(self::date(), $result[3]['Post']['updated']); unset($result[0]['Post']['updated'], $result[1]['Post']['updated']); unset($result[3]['Post']['created'], $result[3]['Post']['updated']); $this->assertEquals($expected, $result); @@ -4400,10 +4392,10 @@ public function testSaveAllValidation() { ) ); - $this->assertTrue($result[0]['Post']['updated'] >= $ts); - $this->assertTrue($result[1]['Post']['updated'] >= $ts); - $this->assertTrue($result[3]['Post']['updated'] >= $ts); - $this->assertTrue($result[3]['Post']['created'] >= $ts); + $this->assertEquals(self::date(), $result[0]['Post']['updated']); + $this->assertEquals(self::date(), $result[1]['Post']['updated']); + $this->assertEquals(self::date(), $result[3]['Post']['updated']); + $this->assertEquals(self::date(), $result[3]['Post']['created']); unset( $result[0]['Post']['updated'], $result[1]['Post']['updated'], $result[3]['Post']['updated'], $result[3]['Post']['created'] @@ -4731,7 +4723,6 @@ public function testSaveAssociated() { $result = $TestModel->find('all'); $this->assertEquals(3, count($result)); $this->assertFalse(isset($result[3])); - $ts = date('Y-m-d H:i:s'); $TestModel->saveAssociated(array( 'Post' => array( @@ -4758,10 +4749,10 @@ public function testSaveAssociated() { 'password' => '5f4dcc3b5aa765d61d8327deb882cf90', 'test' => 'working' )); - $this->assertTrue($result[3]['Post']['updated'] >= $ts); - $this->assertTrue($result[3]['Post']['created'] >= $ts); - $this->assertTrue($result[3]['Author']['created'] >= $ts); - $this->assertTrue($result[3]['Author']['updated'] >= $ts); + $this->assertEquals(self::date(), $result[3]['Post']['updated']); + $this->assertEquals(self::date(), $result[3]['Post']['created']); + $this->assertEquals(self::date(), $result[3]['Author']['created']); + $this->assertEquals(self::date(), $result[3]['Author']['updated']); unset( $result[3]['Post']['updated'], $result[3]['Post']['created'], $result[3]['Author']['updated'], $result[3]['Author']['created'] @@ -4769,10 +4760,7 @@ public function testSaveAssociated() { $this->assertEquals($expected, $result[3]); $this->assertEquals(4, count($result)); - $ts = date('Y-m-d H:i:s'); - $TestModel = new Comment(); - $ts = date('Y-m-d H:i:s'); $result = $TestModel->saveAssociated(array( 'Comment' => array( 'article_id' => 2, @@ -4793,8 +4781,8 @@ public function testSaveAssociated() { 'comment' => 'New comment with attachment', 'published' => 'Y' ); - $this->assertTrue($result[6]['Comment']['updated'] >= $ts); - $this->assertTrue($result[6]['Comment']['created'] >= $ts); + $this->assertEquals(self::date(), $result[6]['Comment']['updated']); + $this->assertEquals(self::date(), $result[6]['Comment']['created']); unset($result[6]['Comment']['updated'], $result[6]['Comment']['created']); $this->assertEquals($expected, $result[6]['Comment']); @@ -4803,8 +4791,8 @@ public function testSaveAssociated() { 'comment_id' => '7', 'attachment' => 'some_file.tgz' ); - $this->assertTrue($result[6]['Attachment']['updated'] >= $ts); - $this->assertTrue($result[6]['Attachment']['created'] >= $ts); + $this->assertEquals(self::date(), $result[6]['Attachment']['updated']); + $this->assertEquals(self::date(), $result[6]['Attachment']['created']); unset($result[6]['Attachment']['updated'], $result[6]['Attachment']['created']); $this->assertEquals($expected, $result[6]['Attachment']); } @@ -4823,7 +4811,6 @@ public function testSaveMany() { // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass $this->db->truncate($TestModel); - $ts = date('Y-m-d H:i:s'); $TestModel->saveMany(array( array( 'title' => 'Multi-record post 1', @@ -4860,10 +4847,10 @@ public function testSaveMany() { ) ) ); - $this->assertTrue($result[0]['Post']['updated'] >= $ts); - $this->assertTrue($result[0]['Post']['created'] >= $ts); - $this->assertTrue($result[1]['Post']['updated'] >= $ts); - $this->assertTrue($result[1]['Post']['created'] >= $ts); + $this->assertEquals(self::date(), $result[0]['Post']['updated']); + $this->assertEquals(self::date(), $result[0]['Post']['created']); + $this->assertEquals(self::date(), $result[1]['Post']['updated']); + $this->assertEquals(self::date(), $result[1]['Post']['created']); unset($result[0]['Post']['updated'], $result[0]['Post']['created']); unset($result[1]['Post']['updated'], $result[1]['Post']['created']); $this->assertEquals($expected, $result); @@ -5415,7 +5402,6 @@ public function testSaveManyTransaction() { array('author_id' => 1, 'title' => 'New Fifth Post'), array('author_id' => 1, 'title' => '') ); - $ts = date('Y-m-d H:i:s'); $this->assertFalse($TestModel->saveMany($data)); $result = $TestModel->find('all', array('recursive' => -1)); @@ -5468,10 +5454,10 @@ public function testSaveManyTransaction() { 'published' => 'N', )); - $this->assertTrue($result[3]['Post']['created'] >= $ts); - $this->assertTrue($result[3]['Post']['updated'] >= $ts); - $this->assertTrue($result[4]['Post']['created'] >= $ts); - $this->assertTrue($result[4]['Post']['updated'] >= $ts); + $this->assertEquals(self::date(), $result[3]['Post']['created']); + $this->assertEquals(self::date(), $result[3]['Post']['updated']); + $this->assertEquals(self::date(), $result[4]['Post']['created']); + $this->assertEquals(self::date(), $result[4]['Post']['updated']); unset($result[3]['Post']['created'], $result[3]['Post']['updated']); unset($result[4]['Post']['created'], $result[4]['Post']['updated']); $this->assertEquals($expected, $result); @@ -5486,7 +5472,6 @@ public function testSaveManyTransaction() { array('author_id' => 1, 'title' => ''), array('author_id' => 1, 'title' => 'New Sixth Post') ); - $ts = date('Y-m-d H:i:s'); $this->assertFalse($TestModel->saveMany($data)); $result = $TestModel->find('all', array('recursive' => -1)); @@ -5538,10 +5523,10 @@ public function testSaveManyTransaction() { 'body' => 'Third Post Body', 'published' => 'N' )); - $this->assertTrue($result[3]['Post']['created'] >= $ts); - $this->assertTrue($result[3]['Post']['updated'] >= $ts); - $this->assertTrue($result[4]['Post']['created'] >= $ts); - $this->assertTrue($result[4]['Post']['updated'] >= $ts); + $this->assertEquals(self::date(), $result[3]['Post']['created']); + $this->assertEquals(self::date(), $result[3]['Post']['updated']); + $this->assertEquals(self::date(), $result[4]['Post']['created']); + $this->assertEquals(self::date(), $result[4]['Post']['updated']); unset($result[3]['Post']['created'], $result[3]['Post']['updated']); unset($result[4]['Post']['created'], $result[4]['Post']['updated']); } @@ -5610,7 +5595,6 @@ public function testSaveManyValidation() { $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment'); $TestModel = new Post(); - $ts = date('Y-m-d H:i:s'); $data = array( array( 'id' => '1', @@ -5673,10 +5657,10 @@ public function testSaveManyValidation() { ) ); - $this->assertTrue($result[0]['Post']['updated'] >= $ts); - $this->assertTrue($result[1]['Post']['updated'] >= $ts); - $this->assertTrue($result[3]['Post']['created'] >= $ts); - $this->assertTrue($result[3]['Post']['updated'] >= $ts); + $this->assertEquals(self::date(), $result[0]['Post']['updated']); + $this->assertEquals(self::date(), $result[1]['Post']['updated']); + $this->assertEquals(self::date(), $result[3]['Post']['created']); + $this->assertEquals(self::date(), $result[3]['Post']['updated']); unset($result[0]['Post']['updated'], $result[1]['Post']['updated']); unset($result[3]['Post']['created'], $result[3]['Post']['updated']); $this->assertEquals($expected, $result); @@ -6373,7 +6357,6 @@ public function testSaveAllFieldListValidateBelongsTo() { $result = $TestModel->find('all'); $this->assertCount(3, $result); $this->assertFalse(isset($result[3])); - $ts = date('Y-m-d H:i:s'); // test belongsTo $fieldList = array( @@ -6399,15 +6382,15 @@ public function testSaveAllFieldListValidateBelongsTo() { 'title' => 'Post without body', 'body' => null, 'published' => 'N', - 'created' => $ts, - 'updated' => $ts, + 'created' => self::date(), + 'updated' => self::date(), ), 'Author' => array ( 'id' => '5', 'user' => 'bob', 'password' => null, - 'created' => $ts, - 'updated' => $ts, + 'created' => self::date(), + 'updated' => self::date(), 'test' => 'working', ), ); @@ -6419,7 +6402,6 @@ public function testSaveAllFieldListValidateBelongsTo() { // test multirecord $this->db->truncate($TestModel); - $ts = date('Y-m-d H:i:s'); $fieldList = array('title', 'author_id'); $TestModel->saveAll(array( array( @@ -6445,8 +6427,8 @@ public function testSaveAllFieldListValidateBelongsTo() { 'title' => 'Multi-record post 1', 'body' => '', 'published' => 'N', - 'created' => $ts, - 'updated' => $ts + 'created' => self::date(), + 'updated' => self::date() ) ), array( @@ -6456,8 +6438,8 @@ public function testSaveAllFieldListValidateBelongsTo() { 'title' => 'Multi-record post 2', 'body' => '', 'published' => 'N', - 'created' => $ts, - 'updated' => $ts + 'created' => self::date(), + 'updated' => self::date() ) ) ); @@ -6545,7 +6527,6 @@ public function testSaveAllDeepFieldListValidateBelongsTo() { $result = $TestModel->find('all'); $this->assertCount(3, $result); $this->assertFalse(isset($result[3])); - $ts = date('Y-m-d H:i:s'); // test belongsTo $fieldList = array( diff --git a/lib/Cake/TestSuite/CakeTestCase.php b/lib/Cake/TestSuite/CakeTestCase.php index 6ef53e3fce4..5b62b6b4567 100644 --- a/lib/Cake/TestSuite/CakeTestCase.php +++ b/lib/Cake/TestSuite/CakeTestCase.php @@ -152,6 +152,16 @@ public function tearDown() { } } +/** + * See CakeTestSuiteDispatcher::date() + * + * @param string $format format to be used. + * @return string + */ + public static function date($format = 'Y-m-d H:i:s') { + return CakeTestSuiteDispatcher::date($format); + } + // @codingStandardsIgnoreStart PHPUnit overrides don't match CakePHP /** diff --git a/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php b/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php index 29b65bfbd73..31267b5731b 100644 --- a/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php +++ b/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php @@ -247,6 +247,7 @@ protected function _runTestCase() { restore_error_handler(); try { + self::time(); $command = new CakeTestSuiteCommand('CakeTestLoader', $commandArgs); $result = $command->run($options); } catch (MissingConnectionException $exception) { @@ -257,4 +258,29 @@ protected function _runTestCase() { } } +/** + * Sets a static timestamp + * + * @param boolean $reset to set new static timestamp. + * @return integer timestamp + */ + public static function time($reset = false) { + static $now; + if ($reset || !$now) { + $now = time(); + } + return $now; + } + +/** + * Returns formatted date string using static time + * This method is being used as formatter for created, modified and updated fields in Model::save() + * + * @param string $format format to be used. + * @return string formatted date + */ + public static function date($format) { + return date($format, self::time()); + } + } diff --git a/lib/Cake/TestSuite/Fixture/CakeTestModel.php b/lib/Cake/TestSuite/Fixture/CakeTestModel.php index dfd8a2bf3d5..b2a83357071 100644 --- a/lib/Cake/TestSuite/Fixture/CakeTestModel.php +++ b/lib/Cake/TestSuite/Fixture/CakeTestModel.php @@ -53,5 +53,18 @@ public function beforeFind($queryData) { } return $queryData; } +/** + * Overriding save() to set CakeTestSuiteDispatcher::date() as formatter for created, modified and updated fields + * + * @param array $data + * @param mixed $validate + * @param array $fieldList + */ + + public function save($data = null, $validate = true, $fieldList = array()) { + $db = $this->getDataSource(); + $db->columns['datetime']['formatter'] = 'CakeTestSuiteDispatcher::date'; + return parent::save($data, $validate, $fieldList); + } -} +} \ No newline at end of file