WhoDidItBehaviorTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. /**
  23. * WhoDidItBehaviorTest::testModel()
  24. *
  25. * @return void
  26. */
  27. public function testModel() {
  28. $this->assertInstanceOf('AppModel', $this->Model);
  29. }
  30. /**
  31. * WhoDidItBehaviorTest::testSaveWithDefaultSettings()
  32. *
  33. * @return void
  34. */
  35. public function testSaveWithDefaultSettings() {
  36. // create (id + name + created + modified)
  37. $data = array(
  38. 'name' => 'Foo'
  39. );
  40. $this->Model->create();
  41. $res = $this->Model->save($data);
  42. $this->assertTrue((bool)$res);
  43. $this->assertTrue(count($res['WhoDidItPlayer']) === 4);
  44. // update (id + name + modified)
  45. $res = $this->Model->save($data + array('id' => $this->Model->id));
  46. $this->assertTrue((bool)$res);
  47. $this->assertTrue(count($res['WhoDidItPlayer']) === 3);
  48. // create a new one being logged in
  49. CakeSession::write('Auth.User.id', '1');
  50. $data = array(
  51. 'name' => 'Foo2'
  52. );
  53. $this->Model->create();
  54. $res = $this->Model->save($data);
  55. $this->assertTrue((bool)$res);
  56. $this->assertTrue(count($res['WhoDidItPlayer']) === 6);
  57. $this->assertEquals('1', $res['WhoDidItPlayer']['created_by']);
  58. $this->assertEquals('1', $res['WhoDidItPlayer']['modified_by']);
  59. // now update being logged in
  60. $res = $this->Model->save($data + array('id' => $this->Model->id));
  61. $this->assertTrue((bool)$res);
  62. $this->assertTrue(count($res['WhoDidItPlayer']) === 4);
  63. $this->assertEquals('1', $res['WhoDidItPlayer']['modified_by']);
  64. }
  65. /**
  66. * Usually the fields modified_by and created_by should not be present in forms.
  67. * For some admin views this might be the case. Here we don't want to modify then
  68. * in most cases. We also don't want the modified timestamp to be wrongly raised.
  69. *
  70. * @return void
  71. */
  72. public function testSaveWithAlreadySetModified() {
  73. CakeSession::write('Auth.User.id', '1');
  74. $data = array(
  75. 'name' => 'Foo'
  76. );
  77. $this->Model->create();
  78. $res = $this->Model->save($data);
  79. $this->assertTrue((bool)$res);
  80. $this->assertTrue(count($res['WhoDidItPlayer']) === 6);
  81. // update (id + name + modified)
  82. CakeSession::write('Auth.User.id', '2');
  83. $data += array('modified_by' => $res['WhoDidItPlayer']['modified_by']);
  84. $res = $this->Model->save($data + array('id' => $this->Model->id));
  85. $this->assertTrue((bool)$res);
  86. $this->assertFalse($res['WhoDidItPlayer']['modified']);
  87. $this->assertTrue(count($res['WhoDidItPlayer']) === 4);
  88. }
  89. }