TimestampBehaviorTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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\Database\Type;
  17. use Cake\Event\Event;
  18. use Cake\I18n\Time;
  19. use Cake\ORM\Behavior\TimestampBehavior;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\TestSuite\TestCase;
  23. use PHPUnit\Framework\Error\Deprecated;
  24. /**
  25. * Behavior test case
  26. */
  27. class TimestampBehaviorTest extends TestCase
  28. {
  29. /**
  30. * autoFixtures
  31. *
  32. * Don't load fixtures for all tests
  33. *
  34. * @var bool
  35. */
  36. public $autoFixtures = false;
  37. /**
  38. * fixtures
  39. *
  40. * @var array
  41. */
  42. public $fixtures = [
  43. 'core.Users',
  44. ];
  45. /**
  46. * Sanity check Implemented events
  47. *
  48. * @return void
  49. */
  50. public function testImplementedEventsDefault()
  51. {
  52. $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  53. $this->Behavior = new TimestampBehavior($table);
  54. $expected = [
  55. 'Model.beforeSave' => 'handleEvent',
  56. ];
  57. $this->assertEquals($expected, $this->Behavior->implementedEvents());
  58. }
  59. /**
  60. * testImplementedEventsCustom
  61. *
  62. * The behavior allows for handling any event - test an example
  63. *
  64. * @return void
  65. */
  66. public function testImplementedEventsCustom()
  67. {
  68. $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  69. $settings = ['events' => ['Something.special' => ['date_specialed' => 'always']]];
  70. $this->Behavior = new TimestampBehavior($table, $settings);
  71. $expected = [
  72. 'Something.special' => 'handleEvent',
  73. ];
  74. $this->assertEquals($expected, $this->Behavior->implementedEvents());
  75. }
  76. /**
  77. * testCreatedAbsent
  78. *
  79. * @return void
  80. * @triggers Model.beforeSave
  81. */
  82. public function testCreatedAbsent()
  83. {
  84. $table = $this->getTable();
  85. $this->Behavior = new TimestampBehavior($table);
  86. $ts = new \DateTime('2000-01-01');
  87. $this->Behavior->timestamp($ts);
  88. $event = new Event('Model.beforeSave');
  89. $entity = new Entity(['name' => 'Foo']);
  90. $return = $this->Behavior->handleEvent($event, $entity);
  91. $this->assertTrue($return, 'Handle Event is expected to always return true');
  92. $this->assertInstanceOf('Cake\I18n\Time', $entity->created);
  93. $this->assertSame($ts->format('c'), $entity->created->format('c'), 'Created timestamp is not the same');
  94. }
  95. /**
  96. * testCreatedPresent
  97. *
  98. * @return void
  99. * @triggers Model.beforeSave
  100. */
  101. public function testCreatedPresent()
  102. {
  103. $table = $this->getTable();
  104. $this->Behavior = new TimestampBehavior($table);
  105. $ts = new \DateTime('2000-01-01');
  106. $this->Behavior->timestamp($ts);
  107. $event = new Event('Model.beforeSave');
  108. $existingValue = new \DateTime('2011-11-11');
  109. $entity = new Entity(['name' => 'Foo', 'created' => $existingValue]);
  110. $return = $this->Behavior->handleEvent($event, $entity);
  111. $this->assertTrue($return, 'Handle Event is expected to always return true');
  112. $this->assertSame($existingValue, $entity->created, 'Created timestamp is expected to be unchanged');
  113. }
  114. /**
  115. * testCreatedNotNew
  116. *
  117. * @return void
  118. * @triggers Model.beforeSave
  119. */
  120. public function testCreatedNotNew()
  121. {
  122. $table = $this->getTable();
  123. $this->Behavior = new TimestampBehavior($table);
  124. $ts = new \DateTime('2000-01-01');
  125. $this->Behavior->timestamp($ts);
  126. $event = new Event('Model.beforeSave');
  127. $entity = new Entity(['name' => 'Foo']);
  128. $entity->setNew(false);
  129. $return = $this->Behavior->handleEvent($event, $entity);
  130. $this->assertTrue($return, 'Handle Event is expected to always return true');
  131. $this->assertNull($entity->created, 'Created timestamp is expected to be untouched if the entity is not new');
  132. }
  133. /**
  134. * testModifiedAbsent
  135. *
  136. * @return void
  137. * @triggers Model.beforeSave
  138. */
  139. public function testModifiedAbsent()
  140. {
  141. $table = $this->getTable();
  142. $this->Behavior = new TimestampBehavior($table);
  143. $ts = new \DateTime('2000-01-01');
  144. $this->Behavior->timestamp($ts);
  145. $event = new Event('Model.beforeSave');
  146. $entity = new Entity(['name' => 'Foo']);
  147. $entity->setNew(false);
  148. $return = $this->Behavior->handleEvent($event, $entity);
  149. $this->assertTrue($return, 'Handle Event is expected to always return true');
  150. $this->assertInstanceOf('Cake\I18n\Time', $entity->modified);
  151. $this->assertSame($ts->format('c'), $entity->modified->format('c'), 'Modified timestamp is not the same');
  152. }
  153. /**
  154. * testModifiedPresent
  155. *
  156. * @return void
  157. * @triggers Model.beforeSave
  158. */
  159. public function testModifiedPresent()
  160. {
  161. $table = $this->getTable();
  162. $this->Behavior = new TimestampBehavior($table);
  163. $ts = new \DateTime('2000-01-01');
  164. $this->Behavior->timestamp($ts);
  165. $event = new Event('Model.beforeSave');
  166. $existingValue = new \DateTime('2011-11-11');
  167. $entity = new Entity(['name' => 'Foo', 'modified' => $existingValue]);
  168. $entity->clean();
  169. $entity->setNew(false);
  170. $return = $this->Behavior->handleEvent($event, $entity);
  171. $this->assertTrue($return, 'Handle Event is expected to always return true');
  172. $this->assertInstanceOf('Cake\I18n\Time', $entity->modified);
  173. $this->assertSame($ts->format('c'), $entity->modified->format('c'), 'Modified timestamp is expected to be updated');
  174. }
  175. /**
  176. * test that timestamp creation doesn't fail on missing columns
  177. *
  178. * @return void
  179. */
  180. public function testModifiedMissingColumn()
  181. {
  182. $table = $this->getTable();
  183. $table->getSchema()->removeColumn('created')->removeColumn('modified');
  184. $this->Behavior = new TimestampBehavior($table);
  185. $ts = new \DateTime('2000-01-01');
  186. $this->Behavior->timestamp($ts);
  187. $event = new Event('Model.beforeSave');
  188. $entity = new Entity(['name' => 'Foo']);
  189. $return = $this->Behavior->handleEvent($event, $entity);
  190. $this->assertTrue($return, 'Handle Event is expected to always return true');
  191. $this->assertNull($entity->created);
  192. $this->assertNull($entity->modified);
  193. }
  194. /**
  195. * testUseImmutable
  196. *
  197. * @return void
  198. * @triggers Model.beforeSave
  199. */
  200. public function testUseImmutable()
  201. {
  202. $table = $this->getTable();
  203. $this->Behavior = new TimestampBehavior($table);
  204. $entity = new Entity();
  205. $event = new Event('Model.beforeSave');
  206. Type::build('timestamp')->useImmutable();
  207. $entity->clean();
  208. $this->Behavior->handleEvent($event, $entity);
  209. $this->assertInstanceOf('Cake\I18n\FrozenTime', $entity->modified);
  210. Type::build('timestamp')->useMutable();
  211. $entity->clean();
  212. $this->Behavior->handleEvent($event, $entity);
  213. $this->assertInstanceOf('Cake\I18n\Time', $entity->modified);
  214. }
  215. /**
  216. * tests using non-DateTimeType throws deprecation warning
  217. *
  218. * @return void
  219. */
  220. public function testNonDateTimeTypeDeprecated()
  221. {
  222. $this->expectException(Deprecated::class);
  223. $this->expectExceptionMessage('TimestampBehavior support for column types other than DateTimeType will be removed in 4.0.');
  224. $table = $this->getTable();
  225. $this->Behavior = new TimestampBehavior($table, [
  226. 'events' => [
  227. 'Model.beforeSave' => [
  228. 'timestamp_str' => 'always',
  229. ],
  230. ],
  231. ]);
  232. $entity = new Entity();
  233. $event = new Event('Model.beforeSave');
  234. $this->Behavior->handleEvent($event, $entity);
  235. $this->assertInternalType('string', $entity->timestamp_str);
  236. }
  237. /**
  238. * testInvalidEventConfig
  239. *
  240. * @return void
  241. * @triggers Model.beforeSave
  242. */
  243. public function testInvalidEventConfig()
  244. {
  245. $this->expectException(\UnexpectedValueException::class);
  246. $this->expectExceptionMessage('When should be one of "always", "new" or "existing". The passed value "fat fingers" is invalid');
  247. $table = $this->getTable();
  248. $settings = ['events' => ['Model.beforeSave' => ['created' => 'fat fingers']]];
  249. $this->Behavior = new TimestampBehavior($table, $settings);
  250. $event = new Event('Model.beforeSave');
  251. $entity = new Entity(['name' => 'Foo']);
  252. $this->Behavior->handleEvent($event, $entity);
  253. }
  254. /**
  255. * testGetTimestamp
  256. *
  257. * @return void
  258. */
  259. public function testGetTimestamp()
  260. {
  261. $table = $this->getTable();
  262. $behavior = new TimestampBehavior($table);
  263. $return = $behavior->timestamp();
  264. $this->assertInstanceOf(
  265. 'DateTime',
  266. $return,
  267. 'Should return a timestamp object'
  268. );
  269. $now = Time::now();
  270. $this->assertEquals($now, $return);
  271. }
  272. /**
  273. * testGetTimestampPersists
  274. *
  275. * @return void
  276. */
  277. public function testGetTimestampPersists()
  278. {
  279. $table = $this->getTable();
  280. $behavior = new TimestampBehavior($table);
  281. $initialValue = $behavior->timestamp();
  282. $postValue = $behavior->timestamp();
  283. $this->assertSame(
  284. $initialValue,
  285. $postValue,
  286. 'The timestamp should be exactly the same object'
  287. );
  288. }
  289. /**
  290. * testGetTimestampRefreshes
  291. *
  292. * @return void
  293. */
  294. public function testGetTimestampRefreshes()
  295. {
  296. $table = $this->getTable();
  297. $behavior = new TimestampBehavior($table);
  298. $initialValue = $behavior->timestamp();
  299. $postValue = $behavior->timestamp(null, true);
  300. $this->assertNotSame(
  301. $initialValue,
  302. $postValue,
  303. 'The timestamp should be a different object if refreshTimestamp is truthy'
  304. );
  305. }
  306. /**
  307. * testSetTimestampExplicit
  308. *
  309. * @return void
  310. */
  311. public function testSetTimestampExplicit()
  312. {
  313. $table = $this->getTable();
  314. $this->Behavior = new TimestampBehavior($table);
  315. $ts = new \DateTime();
  316. $this->Behavior->timestamp($ts);
  317. $return = $this->Behavior->timestamp();
  318. $this->assertEquals(
  319. $ts->format('c'),
  320. $return->format('c'),
  321. 'Should return the same value as initially set'
  322. );
  323. }
  324. /**
  325. * testTouch
  326. *
  327. * @return void
  328. */
  329. public function testTouch()
  330. {
  331. $table = $this->getTable();
  332. $this->Behavior = new TimestampBehavior($table);
  333. $ts = new \DateTime('2000-01-01');
  334. $this->Behavior->timestamp($ts);
  335. $entity = new Entity(['username' => 'timestamp test']);
  336. $return = $this->Behavior->touch($entity);
  337. $this->assertTrue($return, 'touch is expected to return true if it sets a field value');
  338. $this->assertSame(
  339. $ts->format('Y-m-d H:i:s'),
  340. $entity->modified->format('Y-m-d H:i:s'),
  341. 'Modified field is expected to be updated'
  342. );
  343. $this->assertNull($entity->created, 'Created field is NOT expected to change');
  344. }
  345. /**
  346. * testTouchNoop
  347. *
  348. * @return void
  349. */
  350. public function testTouchNoop()
  351. {
  352. $table = $this->getTable();
  353. $config = [
  354. 'events' => [
  355. 'Model.beforeSave' => [
  356. 'created' => 'new',
  357. ],
  358. ],
  359. ];
  360. $this->Behavior = new TimestampBehavior($table, $config);
  361. $ts = new \DateTime('2000-01-01');
  362. $this->Behavior->timestamp($ts);
  363. $entity = new Entity(['username' => 'timestamp test']);
  364. $return = $this->Behavior->touch($entity);
  365. $this->assertFalse($return, 'touch is expected to do nothing and return false');
  366. $this->assertNull($entity->modified, 'Modified field is NOT expected to change');
  367. $this->assertNull($entity->created, 'Created field is NOT expected to change');
  368. }
  369. /**
  370. * testTouchCustomEvent
  371. *
  372. * @return void
  373. */
  374. public function testTouchCustomEvent()
  375. {
  376. $table = $this->getTable();
  377. $settings = ['events' => ['Something.special' => ['date_specialed' => 'always']]];
  378. $this->Behavior = new TimestampBehavior($table, $settings);
  379. $ts = new \DateTime('2000-01-01');
  380. $this->Behavior->timestamp($ts);
  381. $entity = new Entity(['username' => 'timestamp test']);
  382. $return = $this->Behavior->touch($entity, 'Something.special');
  383. $this->assertTrue($return, 'touch is expected to return true if it sets a field value');
  384. $this->assertSame(
  385. $ts->format('Y-m-d H:i:s'),
  386. $entity->date_specialed->format('Y-m-d H:i:s'),
  387. 'Modified field is expected to be updated'
  388. );
  389. $this->assertNull($entity->created, 'Created field is NOT expected to change');
  390. }
  391. /**
  392. * Test that calling save, triggers an insert including the created and updated field values
  393. *
  394. * @return void
  395. */
  396. public function testSaveTriggersInsert()
  397. {
  398. $this->loadFixtures('Users');
  399. $table = $this->getTableLocator()->get('users');
  400. $table->addBehavior('Timestamp', [
  401. 'events' => [
  402. 'Model.beforeSave' => [
  403. 'created' => 'new',
  404. 'updated' => 'always',
  405. ],
  406. ],
  407. ]);
  408. $entity = new Entity(['username' => 'timestamp test']);
  409. $return = $table->save($entity);
  410. $this->assertSame($entity, $return, 'The returned object is expected to be the same entity object');
  411. $row = $table->find('all')->where(['id' => $entity->id])->first();
  412. $now = Time::now();
  413. $this->assertEquals($now->toDateTimeString(), $row->created->toDateTimeString());
  414. $this->assertEquals($now->toDateTimeString(), $row->updated->toDateTimeString());
  415. }
  416. /**
  417. * Helper method to get Table instance with created/modified column
  418. *
  419. * @return \Cake\ORM\Table
  420. */
  421. protected function getTable()
  422. {
  423. $schema = [
  424. 'created' => ['type' => 'datetime'],
  425. 'modified' => ['type' => 'timestamp'],
  426. 'date_specialed' => ['type' => 'datetime'],
  427. 'timestamp_str' => ['type' => 'string'],
  428. ];
  429. $table = new Table(['schema' => $schema]);
  430. return $table;
  431. }
  432. }