TimestampBehaviorTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Model\Behavior;
  16. use Cake\Event\Event;
  17. use Cake\Model\Behavior\TimestampBehavior;
  18. use Cake\ORM\Entity;
  19. use Cake\ORM\TableRegistry;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Utility\Time;
  22. /**
  23. * Behavior test case
  24. */
  25. class TimestampBehaviorTest extends TestCase {
  26. /**
  27. * autoFixtures
  28. *
  29. * Don't load fixtures for all tests
  30. *
  31. * @var bool
  32. */
  33. public $autoFixtures = false;
  34. /**
  35. * fixtures
  36. *
  37. * @var array
  38. */
  39. public $fixtures = [
  40. 'core.user'
  41. ];
  42. /**
  43. * Sanity check Implemented events
  44. *
  45. * @return void
  46. */
  47. public function testImplementedEventsDefault() {
  48. $table = $this->getMock('Cake\ORM\Table');
  49. $this->Behavior = new TimestampBehavior($table);
  50. $expected = [
  51. 'Model.beforeSave' => 'handleEvent'
  52. ];
  53. $this->assertEquals($expected, $this->Behavior->implementedEvents());
  54. }
  55. /**
  56. * testImplementedEventsCustom
  57. *
  58. * The behavior allows for handling any event - test an example
  59. *
  60. * @return void
  61. */
  62. public function testImplementedEventsCustom() {
  63. $table = $this->getMock('Cake\ORM\Table');
  64. $settings = ['events' => ['Something.special' => ['date_specialed' => 'always']]];
  65. $this->Behavior = new TimestampBehavior($table, $settings);
  66. $expected = [
  67. 'Something.special' => 'handleEvent'
  68. ];
  69. $this->assertEquals($expected, $this->Behavior->implementedEvents());
  70. }
  71. /**
  72. * testCreatedAbsent
  73. *
  74. * @return void
  75. */
  76. public function testCreatedAbsent() {
  77. $table = $this->getMock('Cake\ORM\Table');
  78. $this->Behavior = new TimestampBehavior($table);
  79. $ts = new \DateTime('2000-01-01');
  80. $this->Behavior->timestamp($ts);
  81. $event = new Event('Model.beforeSave');
  82. $entity = new Entity(['name' => 'Foo']);
  83. $return = $this->Behavior->handleEvent($event, $entity);
  84. $this->assertTrue($return, 'Handle Event is expected to always return true');
  85. $this->assertInstanceOf('Cake\Utility\Time', $entity->created);
  86. $this->assertSame($ts->format('c'), $entity->created->format('c'), 'Created timestamp is not the same');
  87. }
  88. /**
  89. * testCreatedPresent
  90. *
  91. * @return void
  92. */
  93. public function testCreatedPresent() {
  94. $table = $this->getMock('Cake\ORM\Table');
  95. $this->Behavior = new TimestampBehavior($table);
  96. $ts = new \DateTime('2000-01-01');
  97. $this->Behavior->timestamp($ts);
  98. $event = new Event('Model.beforeSave');
  99. $existingValue = new \DateTime('2011-11-11');
  100. $entity = new Entity(['name' => 'Foo', 'created' => $existingValue]);
  101. $return = $this->Behavior->handleEvent($event, $entity);
  102. $this->assertTrue($return, 'Handle Event is expected to always return true');
  103. $this->assertSame($existingValue, $entity->created, 'Created timestamp is expected to be unchanged');
  104. }
  105. /**
  106. * testCreatedNotNew
  107. *
  108. * @return void
  109. */
  110. public function testCreatedNotNew() {
  111. $table = $this->getMock('Cake\ORM\Table');
  112. $this->Behavior = new TimestampBehavior($table);
  113. $ts = new \DateTime('2000-01-01');
  114. $this->Behavior->timestamp($ts);
  115. $event = new Event('Model.beforeSave');
  116. $entity = new Entity(['name' => 'Foo']);
  117. $entity->isNew(false);
  118. $return = $this->Behavior->handleEvent($event, $entity);
  119. $this->assertTrue($return, 'Handle Event is expected to always return true');
  120. $this->assertNull($entity->created, 'Created timestamp is expected to be untouched if the entity is not new');
  121. }
  122. /**
  123. * testModifiedAbsent
  124. *
  125. * @return void
  126. */
  127. public function testModifiedAbsent() {
  128. $table = $this->getMock('Cake\ORM\Table');
  129. $this->Behavior = new TimestampBehavior($table);
  130. $ts = new \DateTime('2000-01-01');
  131. $this->Behavior->timestamp($ts);
  132. $event = new Event('Model.beforeSave');
  133. $entity = new Entity(['name' => 'Foo']);
  134. $entity->isNew(false);
  135. $return = $this->Behavior->handleEvent($event, $entity);
  136. $this->assertTrue($return, 'Handle Event is expected to always return true');
  137. $this->assertInstanceOf('Cake\Utility\Time', $entity->modified);
  138. $this->assertSame($ts->format('c'), $entity->modified->format('c'), 'Modified timestamp is not the same');
  139. }
  140. /**
  141. * testModifiedPresent
  142. *
  143. * @return void
  144. */
  145. public function testModifiedPresent() {
  146. $table = $this->getMock('Cake\ORM\Table');
  147. $this->Behavior = new TimestampBehavior($table);
  148. $ts = new \DateTime('2000-01-01');
  149. $this->Behavior->timestamp($ts);
  150. $event = new Event('Model.beforeSave');
  151. $existingValue = new \DateTime('2011-11-11');
  152. $entity = new Entity(['name' => 'Foo', 'modified' => $existingValue]);
  153. $entity->clean();
  154. $entity->isNew(false);
  155. $return = $this->Behavior->handleEvent($event, $entity);
  156. $this->assertTrue($return, 'Handle Event is expected to always return true');
  157. $this->assertInstanceOf('Cake\Utility\Time', $entity->modified);
  158. $this->assertSame($ts->format('c'), $entity->modified->format('c'), 'Modified timestamp is expected to be updated');
  159. }
  160. /**
  161. * testInvalidEventConfig
  162. *
  163. * @expectedException UnexpectedValueException
  164. * @expectedExceptionMessage When should be one of "always", "new" or "existing". The passed value "fat fingers" is invalid
  165. * @return void
  166. */
  167. public function testInvalidEventConfig() {
  168. $table = $this->getMock('Cake\ORM\Table');
  169. $settings = ['events' => ['Model.beforeSave' => ['created' => 'fat fingers']]];
  170. $this->Behavior = new TimestampBehavior($table, $settings);
  171. $event = new Event('Model.beforeSave');
  172. $entity = new Entity(['name' => 'Foo']);
  173. $this->Behavior->handleEvent($event, $entity);
  174. }
  175. /**
  176. * testGetTimestamp
  177. *
  178. * @return void
  179. */
  180. public function testGetTimestamp() {
  181. $table = $this->getMock('Cake\ORM\Table');
  182. $this->Behavior = new TimestampBehavior($table);
  183. $return = $this->Behavior->timestamp();
  184. $this->assertInstanceOf(
  185. 'DateTime',
  186. $return,
  187. 'Should return a timestamp object'
  188. );
  189. $now = Time::now();
  190. $this->assertEquals($now, $return);
  191. return $this->Behavior;
  192. }
  193. /**
  194. * testGetTimestampPersists
  195. *
  196. * @depends testGetTimestamp
  197. * @return void
  198. */
  199. public function testGetTimestampPersists($behavior) {
  200. $this->Behavior = $behavior;
  201. $initialValue = $this->Behavior->timestamp();
  202. $postValue = $this->Behavior->timestamp();
  203. $this->assertSame(
  204. $initialValue,
  205. $postValue,
  206. 'The timestamp should be exactly the same object'
  207. );
  208. }
  209. /**
  210. * testGetTimestampRefreshes
  211. *
  212. * @depends testGetTimestamp
  213. * @return void
  214. */
  215. public function testGetTimestampRefreshes($behavior) {
  216. $this->Behavior = $behavior;
  217. $initialValue = $this->Behavior->timestamp();
  218. $postValue = $this->Behavior->timestamp(null, true);
  219. $this->assertNotSame(
  220. $initialValue,
  221. $postValue,
  222. 'The timestamp should be a different object if refreshTimestamp is truthy'
  223. );
  224. }
  225. /**
  226. * testSetTimestampExplicit
  227. *
  228. * @return void
  229. */
  230. public function testSetTimestampExplicit() {
  231. $table = $this->getMock('Cake\ORM\Table');
  232. $this->Behavior = new TimestampBehavior($table);
  233. $ts = new \DateTime();
  234. $this->Behavior->timestamp($ts);
  235. $return = $this->Behavior->timestamp();
  236. $this->assertEquals(
  237. $ts->format('c'),
  238. $return->format('c'),
  239. 'Should return the same value as initially set'
  240. );
  241. }
  242. /**
  243. * testTouch
  244. *
  245. * @return void
  246. */
  247. public function testTouch() {
  248. $table = $this->getMock('Cake\ORM\Table');
  249. $this->Behavior = new TimestampBehavior($table);
  250. $ts = new \DateTime('2000-01-01');
  251. $this->Behavior->timestamp($ts);
  252. $entity = new Entity(['username' => 'timestamp test']);
  253. $return = $this->Behavior->touch($entity);
  254. $this->assertTrue($return, 'touch is expected to return true if it sets a field value');
  255. $this->assertSame(
  256. $ts->format('Y-m-d H:i:s'),
  257. $entity->modified->format('Y-m-d H:i:s'),
  258. 'Modified field is expected to be updated'
  259. );
  260. $this->assertNull($entity->created, 'Created field is NOT expected to change');
  261. }
  262. /**
  263. * testTouchNoop
  264. *
  265. * @return void
  266. */
  267. public function testTouchNoop() {
  268. $table = $this->getMock('Cake\ORM\Table');
  269. $config = [
  270. 'events' => [
  271. 'Model.beforeSave' => [
  272. 'created' => 'new',
  273. ]
  274. ]
  275. ];
  276. $this->Behavior = new TimestampBehavior($table, $config);
  277. $ts = new \DateTime('2000-01-01');
  278. $this->Behavior->timestamp($ts);
  279. $entity = new Entity(['username' => 'timestamp test']);
  280. $return = $this->Behavior->touch($entity);
  281. $this->assertFalse($return, 'touch is expected to do nothing and return false');
  282. $this->assertNull($entity->modified, 'Modified field is NOT expected to change');
  283. $this->assertNull($entity->created, 'Created field is NOT expected to change');
  284. }
  285. /**
  286. * testTouchCustomEvent
  287. *
  288. * @return void
  289. */
  290. public function testTouchCustomEvent() {
  291. $table = $this->getMock('Cake\ORM\Table');
  292. $settings = ['events' => ['Something.special' => ['date_specialed' => 'always']]];
  293. $this->Behavior = new TimestampBehavior($table, $settings);
  294. $ts = new \DateTime('2000-01-01');
  295. $this->Behavior->timestamp($ts);
  296. $entity = new Entity(['username' => 'timestamp test']);
  297. $return = $this->Behavior->touch($entity, 'Something.special');
  298. $this->assertTrue($return, 'touch is expected to return true if it sets a field value');
  299. $this->assertSame(
  300. $ts->format('Y-m-d H:i:s'),
  301. $entity->date_specialed->format('Y-m-d H:i:s'),
  302. 'Modified field is expected to be updated'
  303. );
  304. $this->assertNull($entity->created, 'Created field is NOT expected to change');
  305. }
  306. /**
  307. * Test that calling save, triggers an insert including the created and updated field values
  308. *
  309. * @return void
  310. */
  311. public function testSaveTriggersInsert() {
  312. $this->loadFixtures('User');
  313. $table = TableRegistry::get('users');
  314. $table->addBehavior('Timestamp', [
  315. 'events' => [
  316. 'Model.beforeSave' => [
  317. 'created' => 'new',
  318. 'updated' => 'always'
  319. ]
  320. ]
  321. ]);
  322. $entity = new Entity(['username' => 'timestamp test']);
  323. $return = $table->save($entity);
  324. $this->assertSame($entity, $return, 'The returned object is expected to be the same entity object');
  325. $row = $table->find('all')->where(['id' => $entity->id])->first();
  326. $now = Time::now();
  327. $this->assertEquals($now, $row->created);
  328. $this->assertEquals($now, $row->updated);
  329. }
  330. }