TimestampBehaviorTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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\I18n\Time;
  18. use Cake\Model\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. * 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.users'
  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. * @triggers Model.beforeSave
  76. */
  77. public function testCreatedAbsent() {
  78. $table = $this->getMock('Cake\ORM\Table');
  79. $this->Behavior = new TimestampBehavior($table);
  80. $ts = new \DateTime('2000-01-01');
  81. $this->Behavior->timestamp($ts);
  82. $event = new Event('Model.beforeSave');
  83. $entity = new Entity(['name' => 'Foo']);
  84. $return = $this->Behavior->handleEvent($event, $entity);
  85. $this->assertTrue($return, 'Handle Event is expected to always return true');
  86. $this->assertInstanceOf('Cake\I18n\Time', $entity->created);
  87. $this->assertSame($ts->format('c'), $entity->created->format('c'), 'Created timestamp is not the same');
  88. }
  89. /**
  90. * testCreatedPresent
  91. *
  92. * @return void
  93. * @triggers Model.beforeSave
  94. */
  95. public function testCreatedPresent() {
  96. $table = $this->getMock('Cake\ORM\Table');
  97. $this->Behavior = new TimestampBehavior($table);
  98. $ts = new \DateTime('2000-01-01');
  99. $this->Behavior->timestamp($ts);
  100. $event = new Event('Model.beforeSave');
  101. $existingValue = new \DateTime('2011-11-11');
  102. $entity = new Entity(['name' => 'Foo', 'created' => $existingValue]);
  103. $return = $this->Behavior->handleEvent($event, $entity);
  104. $this->assertTrue($return, 'Handle Event is expected to always return true');
  105. $this->assertSame($existingValue, $entity->created, 'Created timestamp is expected to be unchanged');
  106. }
  107. /**
  108. * testCreatedNotNew
  109. *
  110. * @return void
  111. * @triggers Model.beforeSave
  112. */
  113. public function testCreatedNotNew() {
  114. $table = $this->getMock('Cake\ORM\Table');
  115. $this->Behavior = new TimestampBehavior($table);
  116. $ts = new \DateTime('2000-01-01');
  117. $this->Behavior->timestamp($ts);
  118. $event = new Event('Model.beforeSave');
  119. $entity = new Entity(['name' => 'Foo']);
  120. $entity->isNew(false);
  121. $return = $this->Behavior->handleEvent($event, $entity);
  122. $this->assertTrue($return, 'Handle Event is expected to always return true');
  123. $this->assertNull($entity->created, 'Created timestamp is expected to be untouched if the entity is not new');
  124. }
  125. /**
  126. * testModifiedAbsent
  127. *
  128. * @return void
  129. * @triggers Model.beforeSave
  130. */
  131. public function testModifiedAbsent() {
  132. $table = $this->getMock('Cake\ORM\Table');
  133. $this->Behavior = new TimestampBehavior($table);
  134. $ts = new \DateTime('2000-01-01');
  135. $this->Behavior->timestamp($ts);
  136. $event = new Event('Model.beforeSave');
  137. $entity = new Entity(['name' => 'Foo']);
  138. $entity->isNew(false);
  139. $return = $this->Behavior->handleEvent($event, $entity);
  140. $this->assertTrue($return, 'Handle Event is expected to always return true');
  141. $this->assertInstanceOf('Cake\I18n\Time', $entity->modified);
  142. $this->assertSame($ts->format('c'), $entity->modified->format('c'), 'Modified timestamp is not the same');
  143. }
  144. /**
  145. * testModifiedPresent
  146. *
  147. * @return void
  148. * @triggers Model.beforeSave
  149. */
  150. public function testModifiedPresent() {
  151. $table = $this->getMock('Cake\ORM\Table');
  152. $this->Behavior = new TimestampBehavior($table);
  153. $ts = new \DateTime('2000-01-01');
  154. $this->Behavior->timestamp($ts);
  155. $event = new Event('Model.beforeSave');
  156. $existingValue = new \DateTime('2011-11-11');
  157. $entity = new Entity(['name' => 'Foo', 'modified' => $existingValue]);
  158. $entity->clean();
  159. $entity->isNew(false);
  160. $return = $this->Behavior->handleEvent($event, $entity);
  161. $this->assertTrue($return, 'Handle Event is expected to always return true');
  162. $this->assertInstanceOf('Cake\I18n\Time', $entity->modified);
  163. $this->assertSame($ts->format('c'), $entity->modified->format('c'), 'Modified timestamp is expected to be updated');
  164. }
  165. /**
  166. * testInvalidEventConfig
  167. *
  168. * @expectedException \UnexpectedValueException
  169. * @expectedExceptionMessage When should be one of "always", "new" or "existing". The passed value "fat fingers" is invalid
  170. * @return void
  171. * @triggers Model.beforeSave
  172. */
  173. public function testInvalidEventConfig() {
  174. $table = $this->getMock('Cake\ORM\Table');
  175. $settings = ['events' => ['Model.beforeSave' => ['created' => 'fat fingers']]];
  176. $this->Behavior = new TimestampBehavior($table, $settings);
  177. $event = new Event('Model.beforeSave');
  178. $entity = new Entity(['name' => 'Foo']);
  179. $this->Behavior->handleEvent($event, $entity);
  180. }
  181. /**
  182. * testGetTimestamp
  183. *
  184. * @return void
  185. */
  186. public function testGetTimestamp() {
  187. $table = $this->getMock('Cake\ORM\Table');
  188. $this->Behavior = new TimestampBehavior($table);
  189. $return = $this->Behavior->timestamp();
  190. $this->assertInstanceOf(
  191. 'DateTime',
  192. $return,
  193. 'Should return a timestamp object'
  194. );
  195. $now = Time::now();
  196. $this->assertEquals($now, $return);
  197. return $this->Behavior;
  198. }
  199. /**
  200. * testGetTimestampPersists
  201. *
  202. * @depends testGetTimestamp
  203. * @return void
  204. */
  205. public function testGetTimestampPersists($behavior) {
  206. $this->Behavior = $behavior;
  207. $initialValue = $this->Behavior->timestamp();
  208. $postValue = $this->Behavior->timestamp();
  209. $this->assertSame(
  210. $initialValue,
  211. $postValue,
  212. 'The timestamp should be exactly the same object'
  213. );
  214. }
  215. /**
  216. * testGetTimestampRefreshes
  217. *
  218. * @depends testGetTimestamp
  219. * @return void
  220. */
  221. public function testGetTimestampRefreshes($behavior) {
  222. $this->Behavior = $behavior;
  223. $initialValue = $this->Behavior->timestamp();
  224. $postValue = $this->Behavior->timestamp(null, true);
  225. $this->assertNotSame(
  226. $initialValue,
  227. $postValue,
  228. 'The timestamp should be a different object if refreshTimestamp is truthy'
  229. );
  230. }
  231. /**
  232. * testSetTimestampExplicit
  233. *
  234. * @return void
  235. */
  236. public function testSetTimestampExplicit() {
  237. $table = $this->getMock('Cake\ORM\Table');
  238. $this->Behavior = new TimestampBehavior($table);
  239. $ts = new \DateTime();
  240. $this->Behavior->timestamp($ts);
  241. $return = $this->Behavior->timestamp();
  242. $this->assertEquals(
  243. $ts->format('c'),
  244. $return->format('c'),
  245. 'Should return the same value as initially set'
  246. );
  247. }
  248. /**
  249. * testTouch
  250. *
  251. * @return void
  252. */
  253. public function testTouch() {
  254. $table = $this->getMock('Cake\ORM\Table');
  255. $this->Behavior = new TimestampBehavior($table);
  256. $ts = new \DateTime('2000-01-01');
  257. $this->Behavior->timestamp($ts);
  258. $entity = new Entity(['username' => 'timestamp test']);
  259. $return = $this->Behavior->touch($entity);
  260. $this->assertTrue($return, 'touch is expected to return true if it sets a field value');
  261. $this->assertSame(
  262. $ts->format('Y-m-d H:i:s'),
  263. $entity->modified->format('Y-m-d H:i:s'),
  264. 'Modified field is expected to be updated'
  265. );
  266. $this->assertNull($entity->created, 'Created field is NOT expected to change');
  267. }
  268. /**
  269. * testTouchNoop
  270. *
  271. * @return void
  272. */
  273. public function testTouchNoop() {
  274. $table = $this->getMock('Cake\ORM\Table');
  275. $config = [
  276. 'events' => [
  277. 'Model.beforeSave' => [
  278. 'created' => 'new',
  279. ]
  280. ]
  281. ];
  282. $this->Behavior = new TimestampBehavior($table, $config);
  283. $ts = new \DateTime('2000-01-01');
  284. $this->Behavior->timestamp($ts);
  285. $entity = new Entity(['username' => 'timestamp test']);
  286. $return = $this->Behavior->touch($entity);
  287. $this->assertFalse($return, 'touch is expected to do nothing and return false');
  288. $this->assertNull($entity->modified, 'Modified field is NOT expected to change');
  289. $this->assertNull($entity->created, 'Created field is NOT expected to change');
  290. }
  291. /**
  292. * testTouchCustomEvent
  293. *
  294. * @return void
  295. */
  296. public function testTouchCustomEvent() {
  297. $table = $this->getMock('Cake\ORM\Table');
  298. $settings = ['events' => ['Something.special' => ['date_specialed' => 'always']]];
  299. $this->Behavior = new TimestampBehavior($table, $settings);
  300. $ts = new \DateTime('2000-01-01');
  301. $this->Behavior->timestamp($ts);
  302. $entity = new Entity(['username' => 'timestamp test']);
  303. $return = $this->Behavior->touch($entity, 'Something.special');
  304. $this->assertTrue($return, 'touch is expected to return true if it sets a field value');
  305. $this->assertSame(
  306. $ts->format('Y-m-d H:i:s'),
  307. $entity->date_specialed->format('Y-m-d H:i:s'),
  308. 'Modified field is expected to be updated'
  309. );
  310. $this->assertNull($entity->created, 'Created field is NOT expected to change');
  311. }
  312. /**
  313. * Test that calling save, triggers an insert including the created and updated field values
  314. *
  315. * @return void
  316. */
  317. public function testSaveTriggersInsert() {
  318. $this->loadFixtures('Users');
  319. $table = TableRegistry::get('users');
  320. $table->addBehavior('Timestamp', [
  321. 'events' => [
  322. 'Model.beforeSave' => [
  323. 'created' => 'new',
  324. 'updated' => 'always'
  325. ]
  326. ]
  327. ]);
  328. $entity = new Entity(['username' => 'timestamp test']);
  329. $return = $table->save($entity);
  330. $this->assertSame($entity, $return, 'The returned object is expected to be the same entity object');
  331. $row = $table->find('all')->where(['id' => $entity->id])->first();
  332. $now = Time::now();
  333. $this->assertEquals($now, $row->created);
  334. $this->assertEquals($now, $row->updated);
  335. }
  336. }