TimestampBehaviorTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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\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->getMock('Cake\ORM\Table');
  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->getMock('Cake\ORM\Table');
  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->getMock('Cake\ORM\Table');
  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->getMock('Cake\ORM\Table');
  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->getMock('Cake\ORM\Table');
  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->getMock('Cake\ORM\Table');
  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->getMock('Cake\ORM\Table');
  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. * @expectedException \UnexpectedValueException
  177. * @expectedExceptionMessage When should be one of "always", "new" or "existing". The passed value "fat fingers" is invalid
  178. * @return void
  179. * @triggers Model.beforeSave
  180. */
  181. public function testInvalidEventConfig()
  182. {
  183. $table = $this->getMock('Cake\ORM\Table');
  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->getMock('Cake\ORM\Table');
  198. $this->Behavior = new TimestampBehavior($table);
  199. $return = $this->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. return $this->Behavior;
  208. }
  209. /**
  210. * testGetTimestampPersists
  211. *
  212. * @depends testGetTimestamp
  213. * @return void
  214. */
  215. public function testGetTimestampPersists($behavior)
  216. {
  217. $this->Behavior = $behavior;
  218. $initialValue = $this->Behavior->timestamp();
  219. $postValue = $this->Behavior->timestamp();
  220. $this->assertSame(
  221. $initialValue,
  222. $postValue,
  223. 'The timestamp should be exactly the same object'
  224. );
  225. }
  226. /**
  227. * testGetTimestampRefreshes
  228. *
  229. * @depends testGetTimestamp
  230. * @return void
  231. */
  232. public function testGetTimestampRefreshes($behavior)
  233. {
  234. $this->Behavior = $behavior;
  235. $initialValue = $this->Behavior->timestamp();
  236. $postValue = $this->Behavior->timestamp(null, true);
  237. $this->assertNotSame(
  238. $initialValue,
  239. $postValue,
  240. 'The timestamp should be a different object if refreshTimestamp is truthy'
  241. );
  242. }
  243. /**
  244. * testSetTimestampExplicit
  245. *
  246. * @return void
  247. */
  248. public function testSetTimestampExplicit()
  249. {
  250. $table = $this->getMock('Cake\ORM\Table');
  251. $this->Behavior = new TimestampBehavior($table);
  252. $ts = new \DateTime();
  253. $this->Behavior->timestamp($ts);
  254. $return = $this->Behavior->timestamp();
  255. $this->assertEquals(
  256. $ts->format('c'),
  257. $return->format('c'),
  258. 'Should return the same value as initially set'
  259. );
  260. }
  261. /**
  262. * testTouch
  263. *
  264. * @return void
  265. */
  266. public function testTouch()
  267. {
  268. $table = $this->getMock('Cake\ORM\Table');
  269. $this->Behavior = new TimestampBehavior($table);
  270. $ts = new \DateTime('2000-01-01');
  271. $this->Behavior->timestamp($ts);
  272. $entity = new Entity(['username' => 'timestamp test']);
  273. $return = $this->Behavior->touch($entity);
  274. $this->assertTrue($return, 'touch is expected to return true if it sets a field value');
  275. $this->assertSame(
  276. $ts->format('Y-m-d H:i:s'),
  277. $entity->modified->format('Y-m-d H:i:s'),
  278. 'Modified field is expected to be updated'
  279. );
  280. $this->assertNull($entity->created, 'Created field is NOT expected to change');
  281. }
  282. /**
  283. * testTouchNoop
  284. *
  285. * @return void
  286. */
  287. public function testTouchNoop()
  288. {
  289. $table = $this->getMock('Cake\ORM\Table');
  290. $config = [
  291. 'events' => [
  292. 'Model.beforeSave' => [
  293. 'created' => 'new',
  294. ]
  295. ]
  296. ];
  297. $this->Behavior = new TimestampBehavior($table, $config);
  298. $ts = new \DateTime('2000-01-01');
  299. $this->Behavior->timestamp($ts);
  300. $entity = new Entity(['username' => 'timestamp test']);
  301. $return = $this->Behavior->touch($entity);
  302. $this->assertFalse($return, 'touch is expected to do nothing and return false');
  303. $this->assertNull($entity->modified, 'Modified field is NOT expected to change');
  304. $this->assertNull($entity->created, 'Created field is NOT expected to change');
  305. }
  306. /**
  307. * testTouchCustomEvent
  308. *
  309. * @return void
  310. */
  311. public function testTouchCustomEvent()
  312. {
  313. $table = $this->getMock('Cake\ORM\Table');
  314. $settings = ['events' => ['Something.special' => ['date_specialed' => 'always']]];
  315. $this->Behavior = new TimestampBehavior($table, $settings);
  316. $ts = new \DateTime('2000-01-01');
  317. $this->Behavior->timestamp($ts);
  318. $entity = new Entity(['username' => 'timestamp test']);
  319. $return = $this->Behavior->touch($entity, 'Something.special');
  320. $this->assertTrue($return, 'touch is expected to return true if it sets a field value');
  321. $this->assertSame(
  322. $ts->format('Y-m-d H:i:s'),
  323. $entity->date_specialed->format('Y-m-d H:i:s'),
  324. 'Modified field is expected to be updated'
  325. );
  326. $this->assertNull($entity->created, 'Created field is NOT expected to change');
  327. }
  328. /**
  329. * Test that calling save, triggers an insert including the created and updated field values
  330. *
  331. * @return void
  332. */
  333. public function testSaveTriggersInsert()
  334. {
  335. $this->loadFixtures('Users');
  336. $table = TableRegistry::get('users');
  337. $table->addBehavior('Timestamp', [
  338. 'events' => [
  339. 'Model.beforeSave' => [
  340. 'created' => 'new',
  341. 'updated' => 'always'
  342. ]
  343. ]
  344. ]);
  345. $entity = new Entity(['username' => 'timestamp test']);
  346. $return = $table->save($entity);
  347. $this->assertSame($entity, $return, 'The returned object is expected to be the same entity object');
  348. $row = $table->find('all')->where(['id' => $entity->id])->first();
  349. $now = Time::now();
  350. $this->assertEquals($now, $row->created);
  351. $this->assertEquals($now, $row->updated);
  352. }
  353. }