WhoDidItBehaviorTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. App::uses('WhoDidItBehavior', 'Tools.Model/Behavior');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. class WhoDidItBehaviorTest extends MyCakeTestCase {
  5. /**
  6. * Model for tests
  7. *
  8. * @var
  9. */
  10. public $Model;
  11. /**
  12. * Fixtures for tests
  13. *
  14. * @var array
  15. */
  16. public $fixtures = array('plugin.tools.who_did_it_player', 'core.cake_session');
  17. public function setUp() {
  18. parent::setUp();
  19. $this->Model = ClassRegistry::init('WhoDidItPlayer');
  20. $this->Model->Behaviors->load('Tools.WhoDidIt');
  21. }
  22. public function testModel() {
  23. $this->assertInstanceOf('AppModel', $this->Model);
  24. }
  25. public function testSaveWithDefaultSettings() {
  26. // create (id + name + created + modified)
  27. $data = array(
  28. 'name' => 'Foo'
  29. );
  30. $this->Model->create();
  31. $res = $this->Model->save($data);
  32. $this->assertTrue((bool)$res);
  33. $this->assertTrue(count($res['WhoDidItPlayer']) === 4);
  34. // update (id + name + modified)
  35. $res = $this->Model->save($data + array('id' => $this->Model->id));
  36. $this->assertTrue((bool)$res);
  37. $this->assertTrue(count($res['WhoDidItPlayer']) === 3);
  38. // create a new one being logged in
  39. CakeSession::write('Auth.User.id', '1');
  40. $data = array(
  41. 'name' => 'Foo2'
  42. );
  43. $this->Model->create();
  44. $res = $this->Model->save($data);
  45. $this->assertTrue((bool)$res);
  46. $this->assertTrue(count($res['WhoDidItPlayer']) === 6);
  47. $this->assertEquals('1', $res['WhoDidItPlayer']['created_by']);
  48. $this->assertEquals('1', $res['WhoDidItPlayer']['modified_by']);
  49. // now update being logged in
  50. $res = $this->Model->save($data + array('id' => $this->Model->id));
  51. $this->assertTrue((bool)$res);
  52. $this->assertTrue(count($res['WhoDidItPlayer']) === 4);
  53. $this->assertEquals('1', $res['WhoDidItPlayer']['modified_by']);
  54. }
  55. /**
  56. * Usually the fields modified_by and created_by should not be present in forms.
  57. * For some admin views this might be the case. Here we don't want to modify then
  58. * in most cases. We also don't want the modified timestamp to be wrongly raised.
  59. *
  60. * @return void
  61. */
  62. public function testSaveWithAlreadySetModified() {
  63. CakeSession::write('Auth.User.id', '1');
  64. $data = array(
  65. 'name' => 'Foo'
  66. );
  67. $this->Model->create();
  68. $res = $this->Model->save($data);
  69. $this->assertTrue((bool)$res);
  70. $this->assertTrue(count($res['WhoDidItPlayer']) === 6);
  71. // update (id + name + modified)
  72. CakeSession::write('Auth.User.id', '2');
  73. $data += array('modified_by' => $res['WhoDidItPlayer']['modified_by']);
  74. $res = $this->Model->save($data + array('id' => $this->Model->id));
  75. $this->assertTrue((bool)$res);
  76. $this->assertFalse($res['WhoDidItPlayer']['modified']);
  77. $this->assertTrue(count($res['WhoDidItPlayer']) === 4);
  78. }
  79. }