diff --git a/Test/Case/Behavior/AuditableBehaviorTest.php b/Test/Case/Behavior/AuditableBehaviorTest.php index 61c25ea..0672b04 100644 --- a/Test/Case/Behavior/AuditableBehaviorTest.php +++ b/Test/Case/Behavior/AuditableBehaviorTest.php @@ -1,68 +1,9 @@ array( - 'ignore' => array('ignored_field'), - ), - ); - -/** - * Belongs to relationships - * - * @var array - */ - public $belongsTo = array('Author'); -} - -/** - * Author test model - */ -class Author extends CakeTestModel { - -/** - * The name of the model - * - * @var string - */ - public $name = 'Author'; - -/** - * The behaviors - * - * @var array - */ - public $actsAs = array( - 'AuditLog.Auditable', - ); - -/** - * Has many relationships - * - * @var array - */ - public $hasMany = array('Article'); -} +require_once dirname(__FILE__) . DS . 'models.php'; /** * AuditableBehavior Tests @@ -77,6 +18,8 @@ class AuditableBehaviorTest extends CakeTestCase { public $fixtures = array( 'plugin.audit_log.article', 'plugin.audit_log.author', + 'plugin.audit_log.tag', + 'plugin.audit_log.articles_tag', 'plugin.audit_log.audit', 'plugin.audit_log.audit_delta', ); @@ -88,13 +31,15 @@ class AuditableBehaviorTest extends CakeTestCase { */ public function setUp() { $this->Article = ClassRegistry::init('Article'); + $this->Tag = ClassRegistry::init('Tag'); + $this->Audit = ClassRegistry::init('AuditLog.Audit'); + $this->AuditDelta = ClassRegistry::init('AuditLog.AuditDelta'); } /** * Test the action of creating a new record. * * @return void - * @todo Test HABTM save. */ public function testCreate() { $newArticle = array( @@ -108,7 +53,7 @@ public function testCreate() { ); $this->Article->save($newArticle); - $audit = ClassRegistry::init('AuditLog.Audit')->find( + $audit = $this->Audit->find( 'first', array( 'recursive' => -1, @@ -121,7 +66,7 @@ public function testCreate() { ); $article = json_decode($audit['Audit']['json_object'], true); - $deltas = ClassRegistry::init('AuditLog.AuditDelta')->find( + $deltas = $this->AuditDelta->find( 'all', array( 'recursive' => -1, @@ -135,14 +80,13 @@ public function testCreate() { $this->assertEquals('N', $article['Article']['published']); // Verify that delta record were created, too. - $this->assertCount(6, $deltas); + $this->assertCount(7, $deltas); } /** * Test the action of creating a new record when some values are empty. * * @return void - * @todo Test HABTM save. */ public function testCreateWithEmptyValues() { $newArticle = array( @@ -156,7 +100,7 @@ public function testCreateWithEmptyValues() { ); $this->Article->save($newArticle); - $audit = ClassRegistry::init('AuditLog.Audit')->find( + $audit = $this->Audit->find( 'first', array( 'recursive' => -1, @@ -169,7 +113,7 @@ public function testCreateWithEmptyValues() { ); $article = json_decode($audit['Audit']['json_object'], true); - $deltas = ClassRegistry::init('AuditLog.AuditDelta')->find( + $deltas = $this->AuditDelta->find( 'all', array( 'recursive' => -1, @@ -183,75 +127,80 @@ public function testCreateWithEmptyValues() { $this->assertEquals('N', $article['Article']['published']); // Verify that delta record were created, too. - $this->assertCount(6, $deltas); + $this->assertCount(7, $deltas); } /** - * Test saving multiple records with Model::saveAll() + * Test saving associated records with Model::saveAll() * * @return void */ - public function testSaveAll() { - // Test a model and a single associated model. - $data = array( - 'Article' => array( - 'user_id' => 1, - 'title' => 'Rob\'s Test Article', - 'body' => 'Rob\'s Test Article Body', - 'published' => 'Y', - ), - 'Author' => array( - 'first_name' => 'Rob', - 'last_name' => 'Wilkerson', - ), - ); + public function testSaveAssociated() + { + // Test a model and a single associated model. + $data = array( + 'Article' => array( + 'user_id' => 1, + 'title' => 'Rob\'s Test Article', + 'body' => 'Rob\'s Test Article Body', + 'published' => 'Y', + ), + 'Author' => array( + 'first_name' => 'Rob', + 'last_name' => 'Wilkerson', + ), + ); + + $this->Article->saveAll($data); + + $articleAudit = $this->Audit->find( + 'first', + array( + 'recursive' => 1, + 'conditions' => array( + 'Audit.event' => 'CREATE', + 'Audit.model' => 'Article', + 'Audit.entity_id' => $this->Article->getLastInsertId(), + ), + ) + ); + $article = json_decode($articleAudit['Audit']['json_object'], true); + + // Verify the audit record. + $this->assertEquals(1, $article['Article']['user_id']); + $this->assertEquals('Rob\'s Test Article', $article['Article']['title']); + $this->assertEquals('Y', $article['Article']['published']); + + // Verify the delta records were created. + $this->assertCount(7, $articleAudit['AuditDelta']); + + $authorAudit = $this->Audit->find( + 'first', + array( + 'recursive' => 1, + 'conditions' => array( + 'Audit.event' => 'CREATE', + 'Audit.model' => 'Author', + 'Audit.entity_id' => $this->Article->Author->getLastInsertId(), + ), + ) + ); + $author = json_decode($authorAudit['Audit']['json_object'], true); + + // Verify the audit record. + $this->assertEquals($article['Article']['author_id'], $author['Author']['id']); + $this->assertEquals('Rob', $author['Author']['first_name']); + + // Verify the delta records were created. + $this->assertCount(3, $authorAudit['AuditDelta']); + } - $this->Article->saveAll($data); - - $auditModel = ClassRegistry::init('AuditLog.Audit'); - - $articleAudit = $auditModel->find( - 'first', - array( - 'recursive' => 1, - 'conditions' => array( - 'Audit.event' => 'CREATE', - 'Audit.model' => 'Article', - 'Audit.entity_id' => $this->Article->getLastInsertId(), - ), - ) - ); - $article = json_decode($articleAudit['Audit']['json_object'], true); - - // Verify the audit record. - $this->assertEquals(1, $article['Article']['user_id']); - $this->assertEquals('Rob\'s Test Article', $article['Article']['title']); - $this->assertEquals('Y', $article['Article']['published']); - - // Verify the delta records were created. - $this->assertCount(6, $articleAudit['AuditDelta']); - - $authorAudit = $auditModel->find( - 'first', - array( - 'recursive' => 1, - 'conditions' => array( - 'Audit.event' => 'CREATE', - 'Audit.model' => 'Author', - 'Audit.entity_id' => $this->Article->Author->getLastInsertId(), - ), - ) - ); - $author = json_decode($authorAudit['Audit']['json_object'], true); - - // Verify the audit record. - $this->assertEquals($article['Article']['author_id'], $author['Author']['id']); - $this->assertEquals('Rob', $author['Author']['first_name']); - - // Verify the delta records were created. - $this->assertCount(3, $authorAudit['AuditDelta']); - - // Test multiple records of one model. +/** + * Test saving multiple records with Model::saveAll() + * + * @return void + */ + public function testSaveMany() { $data = array( array( 'Article' => array( @@ -287,7 +236,7 @@ public function testSaveAll() { $this->Article->saveAll($data); // Retrieve the audits for the last 3 articles saved. - $audits = $auditModel->find( + $audits = $this->Audit->find( 'all', array( 'conditions' => array( @@ -317,9 +266,9 @@ public function testSaveAll() { $this->assertEquals('Y', $article3['Article']['published']); // Verify that no delta records were created. - $this->assertCount(6, $audits[0]['AuditDelta']); - $this->assertCount(6, $audits[1]['AuditDelta']); - $this->assertCount(6, $audits[2]['AuditDelta']); + $this->assertCount(7, $audits[0]['AuditDelta']); + $this->assertCount(7, $audits[1]['AuditDelta']); + $this->assertCount(7, $audits[2]['AuditDelta']); } /** @@ -330,9 +279,6 @@ public function testSaveAll() { * @todo Test HABTM save. */ public function testEdit() { - $this->Audit = ClassRegistry::init('AuditLog.Audit'); - $this->AuditDelta = ClassRegistry::init('AuditLog.AuditDelta'); - $newArticle = array( 'Article' => array( 'user_id' => 1, @@ -455,9 +401,6 @@ public function testEdit() { * @return void */ public function testIgnoredField() { - $this->Audit = ClassRegistry::init('AuditLog.Audit'); - $this->AuditDelta = ClassRegistry::init('AuditLog.AuditDelta'); - $newArticle = array( 'Article' => array( 'user_id' => 1, @@ -496,8 +439,6 @@ public function testIgnoredField() { * @return void */ public function testDelete() { - $this->Audit = ClassRegistry::init('AuditLog.Audit'); - $this->AuditDelta = ClassRegistry::init('AuditLog.AuditDelta'); $article = $this->Article->find( 'first', array( diff --git a/Test/Case/Behavior/models.php b/Test/Case/Behavior/models.php new file mode 100644 index 0000000..c69daf0 --- /dev/null +++ b/Test/Case/Behavior/models.php @@ -0,0 +1,117 @@ + array( + 'ignore' => array('ignored_field'), + ), + ); + + /** + * Belongs to relationships + * + * @var array + */ + public $belongsTo = array('Author'); + + public $hasAndBelongsToMany = array( + 'Tag' => array( + 'className' => 'Tag', + 'joinTable' => 'articles_tags', + 'foreignKey' => 'article_id', + 'associationForeignKey' => 'tag_id' + ) + ); +} + +/** + * Author test model + */ +class Author extends CakeTestModel { + + /** + * The name of the model + * + * @var string + */ + public $name = 'Author'; + + /** + * The behaviors + * + * @var array + */ + public $actsAs = array( + 'AuditLog.Auditable', + ); + + /** + * Has many relationships + * + * @var array + */ + public $hasMany = array('Article'); +} + + +/** + * Class TagB- Habtm Test Model + */ +class Tag extends CakeTestModel { + + /** + * The name of the model + * + * @var string + */ + public $name = 'Tag'; + + /** + * Table of the model + * + * @var string + */ + public $useTable = 'tags'; + + /** + * The behaviors + * + * @var array + */ + public $actsAs = array( + 'AuditLog.Auditable', + ); + + /** + * HasAndBelongsToMany relationships + * + * @var array + */ + public $hasAndBelongsToMany = array( + 'Article' => array( + 'className' => 'Article', + 'joinTable' => 'articles_tags', + 'foreignKey' => 'tag_id', + 'associationForeignKey' => 'article_id' + ) + ); + +} \ No newline at end of file diff --git a/Test/Fixture/ArticlesTagFixture.php b/Test/Fixture/ArticlesTagFixture.php new file mode 100644 index 0000000..5f3a0d5 --- /dev/null +++ b/Test/Fixture/ArticlesTagFixture.php @@ -0,0 +1,48 @@ + + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt + * Redistributions of files must retain the above copyright notice + * + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests + * @package Cake.Test.Fixture + * @since CakePHP(tm) v 1.2.0.4667 + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ + +/** + * Short description for class. + * + * @package Cake.Test.Fixture + */ +class ArticlesTagFixture extends CakeTestFixture { + +/** + * fields property + * + * @var array + */ + public $fields = array( + 'article_id' => array('type' => 'integer', 'null' => false), + 'tag_id' => array('type' => 'integer', 'null' => false), + 'indexes' => array('UNIQUE_TAG2' => array('column' => array('article_id', 'tag_id'), 'unique' => 1)) + ); + +/** + * records property + * + * @var array + */ + public $records = array( + array('article_id' => 1, 'tag_id' => 1), + array('article_id' => 1, 'tag_id' => 2), + array('article_id' => 2, 'tag_id' => 1), + array('article_id' => 2, 'tag_id' => 3) + ); +} diff --git a/Test/Fixture/TagFixture.php b/Test/Fixture/TagFixture.php new file mode 100644 index 0000000..cee5ceb --- /dev/null +++ b/Test/Fixture/TagFixture.php @@ -0,0 +1,48 @@ + + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt + * Redistributions of files must retain the above copyright notice + * + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests + * @package Cake.Test.Fixture + * @since CakePHP(tm) v 1.2.0.4667 + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ + +/** + * TagFixture + * + * @package Cake.Test.Fixture + */ +class TagFixture extends CakeTestFixture { + +/** + * fields property + * + * @var array + */ + public $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary'), + 'tag' => array('type' => 'string', 'null' => false), + 'created' => array('type' => 'datetime', 'null' => true, 'default' => null), + 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null), + ); + +/** + * records property + * + * @var array + */ + public $records = array( + array('tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), + array('tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'), + array('tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31') + ); +}