| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Model\Behavior;
- use Cake\Event\Event;
- use Cake\Model\Behavior\TimestampBehavior;
- use Cake\ORM\Entity;
- use Cake\ORM\TableRegistry;
- use Cake\TestSuite\TestCase;
- use Cake\Utility\Time;
- /**
- * Behavior test case
- */
- class TimestampBehaviorTest extends TestCase {
- /**
- * autoFixtures
- *
- * Don't load fixtures for all tests
- *
- * @var bool
- */
- public $autoFixtures = false;
- /**
- * fixtures
- *
- * @var array
- */
- public $fixtures = [
- 'core.user'
- ];
- /**
- * Sanity check Implemented events
- *
- * @return void
- */
- public function testImplementedEventsDefault() {
- $table = $this->getMock('Cake\ORM\Table');
- $this->Behavior = new TimestampBehavior($table);
- $expected = [
- 'Model.beforeSave' => 'handleEvent'
- ];
- $this->assertEquals($expected, $this->Behavior->implementedEvents());
- }
- /**
- * testImplementedEventsCustom
- *
- * The behavior allows for handling any event - test an example
- *
- * @return void
- */
- public function testImplementedEventsCustom() {
- $table = $this->getMock('Cake\ORM\Table');
- $settings = ['events' => ['Something.special' => ['date_specialed' => 'always']]];
- $this->Behavior = new TimestampBehavior($table, $settings);
- $expected = [
- 'Something.special' => 'handleEvent'
- ];
- $this->assertEquals($expected, $this->Behavior->implementedEvents());
- }
- /**
- * testCreatedAbsent
- *
- * @return void
- */
- public function testCreatedAbsent() {
- $table = $this->getMock('Cake\ORM\Table');
- $this->Behavior = new TimestampBehavior($table);
- $ts = new \DateTime('2000-01-01');
- $this->Behavior->timestamp($ts);
- $event = new Event('Model.beforeSave');
- $entity = new Entity(['name' => 'Foo']);
- $return = $this->Behavior->handleEvent($event, $entity);
- $this->assertTrue($return, 'Handle Event is expected to always return true');
- $this->assertInstanceOf('Cake\Utility\Time', $entity->created);
- $this->assertSame($ts->format('c'), $entity->created->format('c'), 'Created timestamp is not the same');
- }
- /**
- * testCreatedPresent
- *
- * @return void
- */
- public function testCreatedPresent() {
- $table = $this->getMock('Cake\ORM\Table');
- $this->Behavior = new TimestampBehavior($table);
- $ts = new \DateTime('2000-01-01');
- $this->Behavior->timestamp($ts);
- $event = new Event('Model.beforeSave');
- $existingValue = new \DateTime('2011-11-11');
- $entity = new Entity(['name' => 'Foo', 'created' => $existingValue]);
- $return = $this->Behavior->handleEvent($event, $entity);
- $this->assertTrue($return, 'Handle Event is expected to always return true');
- $this->assertSame($existingValue, $entity->created, 'Created timestamp is expected to be unchanged');
- }
- /**
- * testCreatedNotNew
- *
- * @return void
- */
- public function testCreatedNotNew() {
- $table = $this->getMock('Cake\ORM\Table');
- $this->Behavior = new TimestampBehavior($table);
- $ts = new \DateTime('2000-01-01');
- $this->Behavior->timestamp($ts);
- $event = new Event('Model.beforeSave');
- $entity = new Entity(['name' => 'Foo']);
- $entity->isNew(false);
- $return = $this->Behavior->handleEvent($event, $entity);
- $this->assertTrue($return, 'Handle Event is expected to always return true');
- $this->assertNull($entity->created, 'Created timestamp is expected to be untouched if the entity is not new');
- }
- /**
- * testModifiedAbsent
- *
- * @return void
- */
- public function testModifiedAbsent() {
- $table = $this->getMock('Cake\ORM\Table');
- $this->Behavior = new TimestampBehavior($table);
- $ts = new \DateTime('2000-01-01');
- $this->Behavior->timestamp($ts);
- $event = new Event('Model.beforeSave');
- $entity = new Entity(['name' => 'Foo']);
- $entity->isNew(false);
- $return = $this->Behavior->handleEvent($event, $entity);
- $this->assertTrue($return, 'Handle Event is expected to always return true');
- $this->assertInstanceOf('Cake\Utility\Time', $entity->modified);
- $this->assertSame($ts->format('c'), $entity->modified->format('c'), 'Modified timestamp is not the same');
- }
- /**
- * testModifiedPresent
- *
- * @return void
- */
- public function testModifiedPresent() {
- $table = $this->getMock('Cake\ORM\Table');
- $this->Behavior = new TimestampBehavior($table);
- $ts = new \DateTime('2000-01-01');
- $this->Behavior->timestamp($ts);
- $event = new Event('Model.beforeSave');
- $existingValue = new \DateTime('2011-11-11');
- $entity = new Entity(['name' => 'Foo', 'modified' => $existingValue]);
- $entity->clean();
- $entity->isNew(false);
- $return = $this->Behavior->handleEvent($event, $entity);
- $this->assertTrue($return, 'Handle Event is expected to always return true');
- $this->assertInstanceOf('Cake\Utility\Time', $entity->modified);
- $this->assertSame($ts->format('c'), $entity->modified->format('c'), 'Modified timestamp is expected to be updated');
- }
- /**
- * testInvalidEventConfig
- *
- * @expectedException UnexpectedValueException
- * @expectedExceptionMessage When should be one of "always", "new" or "existing". The passed value "fat fingers" is invalid
- * @return void
- */
- public function testInvalidEventConfig() {
- $table = $this->getMock('Cake\ORM\Table');
- $settings = ['events' => ['Model.beforeSave' => ['created' => 'fat fingers']]];
- $this->Behavior = new TimestampBehavior($table, $settings);
- $event = new Event('Model.beforeSave');
- $entity = new Entity(['name' => 'Foo']);
- $this->Behavior->handleEvent($event, $entity);
- }
- /**
- * testGetTimestamp
- *
- * @return void
- */
- public function testGetTimestamp() {
- $table = $this->getMock('Cake\ORM\Table');
- $this->Behavior = new TimestampBehavior($table);
- $return = $this->Behavior->timestamp();
- $this->assertInstanceOf(
- 'DateTime',
- $return,
- 'Should return a timestamp object'
- );
- $now = Time::now();
- $this->assertEquals($now, $return);
- return $this->Behavior;
- }
- /**
- * testGetTimestampPersists
- *
- * @depends testGetTimestamp
- * @return void
- */
- public function testGetTimestampPersists($behavior) {
- $this->Behavior = $behavior;
- $initialValue = $this->Behavior->timestamp();
- $postValue = $this->Behavior->timestamp();
- $this->assertSame(
- $initialValue,
- $postValue,
- 'The timestamp should be exactly the same object'
- );
- }
- /**
- * testGetTimestampRefreshes
- *
- * @depends testGetTimestamp
- * @return void
- */
- public function testGetTimestampRefreshes($behavior) {
- $this->Behavior = $behavior;
- $initialValue = $this->Behavior->timestamp();
- $postValue = $this->Behavior->timestamp(null, true);
- $this->assertNotSame(
- $initialValue,
- $postValue,
- 'The timestamp should be a different object if refreshTimestamp is truthy'
- );
- }
- /**
- * testSetTimestampExplicit
- *
- * @return void
- */
- public function testSetTimestampExplicit() {
- $table = $this->getMock('Cake\ORM\Table');
- $this->Behavior = new TimestampBehavior($table);
- $ts = new \DateTime();
- $this->Behavior->timestamp($ts);
- $return = $this->Behavior->timestamp();
- $this->assertEquals(
- $ts->format('c'),
- $return->format('c'),
- 'Should return the same value as initially set'
- );
- }
- /**
- * testTouch
- *
- * @return void
- */
- public function testTouch() {
- $table = $this->getMock('Cake\ORM\Table');
- $this->Behavior = new TimestampBehavior($table);
- $ts = new \DateTime('2000-01-01');
- $this->Behavior->timestamp($ts);
- $entity = new Entity(['username' => 'timestamp test']);
- $return = $this->Behavior->touch($entity);
- $this->assertTrue($return, 'touch is expected to return true if it sets a field value');
- $this->assertSame(
- $ts->format('Y-m-d H:i:s'),
- $entity->modified->format('Y-m-d H:i:s'),
- 'Modified field is expected to be updated'
- );
- $this->assertNull($entity->created, 'Created field is NOT expected to change');
- }
- /**
- * testTouchNoop
- *
- * @return void
- */
- public function testTouchNoop() {
- $table = $this->getMock('Cake\ORM\Table');
- $config = [
- 'events' => [
- 'Model.beforeSave' => [
- 'created' => 'new',
- ]
- ]
- ];
- $this->Behavior = new TimestampBehavior($table, $config);
- $ts = new \DateTime('2000-01-01');
- $this->Behavior->timestamp($ts);
- $entity = new Entity(['username' => 'timestamp test']);
- $return = $this->Behavior->touch($entity);
- $this->assertFalse($return, 'touch is expected to do nothing and return false');
- $this->assertNull($entity->modified, 'Modified field is NOT expected to change');
- $this->assertNull($entity->created, 'Created field is NOT expected to change');
- }
- /**
- * testTouchCustomEvent
- *
- * @return void
- */
- public function testTouchCustomEvent() {
- $table = $this->getMock('Cake\ORM\Table');
- $settings = ['events' => ['Something.special' => ['date_specialed' => 'always']]];
- $this->Behavior = new TimestampBehavior($table, $settings);
- $ts = new \DateTime('2000-01-01');
- $this->Behavior->timestamp($ts);
- $entity = new Entity(['username' => 'timestamp test']);
- $return = $this->Behavior->touch($entity, 'Something.special');
- $this->assertTrue($return, 'touch is expected to return true if it sets a field value');
- $this->assertSame(
- $ts->format('Y-m-d H:i:s'),
- $entity->date_specialed->format('Y-m-d H:i:s'),
- 'Modified field is expected to be updated'
- );
- $this->assertNull($entity->created, 'Created field is NOT expected to change');
- }
- /**
- * Test that calling save, triggers an insert including the created and updated field values
- *
- * @return void
- */
- public function testSaveTriggersInsert() {
- $this->loadFixtures('User');
- $table = TableRegistry::get('users');
- $table->addBehavior('Timestamp', [
- 'events' => [
- 'Model.beforeSave' => [
- 'created' => 'new',
- 'updated' => 'always'
- ]
- ]
- ]);
- $entity = new Entity(['username' => 'timestamp test']);
- $return = $table->save($entity);
- $this->assertSame($entity, $return, 'The returned object is expected to be the same entity object');
- $row = $table->find('all')->where(['id' => $entity->id])->first();
- $now = Time::now();
- $this->assertEquals($now, $row->created);
- $this->assertEquals($now, $row->updated);
- }
- }
|