TimestampBehaviorTest.php 10 KB

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