| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- App::uses('WhoDidItBehavior', 'Tools.Model/Behavior');
- App::uses('MyCakeTestCase', 'Tools.TestSuite');
- class WhoDidItBehaviorTest extends MyCakeTestCase {
- /**
- * Model for tests
- *
- * @var
- */
- public $Model;
- /**
- * Fixtures for tests
- *
- * @var array
- */
- public $fixtures = ['plugin.tools.who_did_it_player', 'core.cake_session'];
- public function setUp() {
- parent::setUp();
- $this->Model = ClassRegistry::init('WhoDidItPlayer');
- $this->Model->Behaviors->load('Tools.WhoDidIt');
- }
- /**
- * WhoDidItBehaviorTest::testModel()
- *
- * @return void
- */
- public function testModel() {
- $this->assertInstanceOf('AppModel', $this->Model);
- }
- /**
- * WhoDidItBehaviorTest::testSaveWithDefaultSettings()
- *
- * @return void
- */
- public function testSaveWithDefaultSettings() {
- // create (id + name + created + modified)
- $data = [
- 'name' => 'Foo'
- ];
- $this->Model->create();
- $res = $this->Model->save($data);
- $this->assertTrue((bool)$res);
- $this->assertTrue(count($res['WhoDidItPlayer']) === 4);
- // update (id + name + modified)
- $res = $this->Model->save($data + ['id' => $this->Model->id]);
- $this->assertTrue((bool)$res);
- $this->assertTrue(count($res['WhoDidItPlayer']) === 3);
- // create a new one being logged in
- CakeSession::write('Auth.User.id', '1');
- $data = [
- 'name' => 'Foo2'
- ];
- $this->Model->create();
- $res = $this->Model->save($data);
- $this->assertTrue((bool)$res);
- $this->assertTrue(count($res['WhoDidItPlayer']) === 6);
- $this->assertEquals('1', $res['WhoDidItPlayer']['created_by']);
- $this->assertEquals('1', $res['WhoDidItPlayer']['modified_by']);
- // now update being logged in
- $res = $this->Model->save($data + ['id' => $this->Model->id]);
- $this->assertTrue((bool)$res);
- $this->assertTrue(count($res['WhoDidItPlayer']) === 4);
- $this->assertEquals('1', $res['WhoDidItPlayer']['modified_by']);
- }
- /**
- * Usually the fields modified_by and created_by should not be present in forms.
- * For some admin views this might be the case. Here we don't want to modify then
- * in most cases. We also don't want the modified timestamp to be wrongly raised.
- *
- * @return void
- */
- public function testSaveWithAlreadySetModified() {
- CakeSession::write('Auth.User.id', '1');
- $data = [
- 'name' => 'Foo'
- ];
- $this->Model->create();
- $res = $this->Model->save($data);
- $this->assertTrue((bool)$res);
- $this->assertTrue(count($res['WhoDidItPlayer']) === 6);
- // update (id + name + modified)
- CakeSession::write('Auth.User.id', '2');
- $data += ['modified_by' => $res['WhoDidItPlayer']['modified_by']];
- $res = $this->Model->save($data + ['id' => $this->Model->id]);
- $this->assertTrue((bool)$res);
- $this->assertFalse($res['WhoDidItPlayer']['modified']);
- $this->assertTrue(count($res['WhoDidItPlayer']) === 4);
- }
- }
|