TimestampBehaviorTest.php 13 KB

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