From cf96043d9ebf21a310e2579b63510fbc13eda8a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Sun, 22 Jun 2014 03:04:02 +0200 Subject: [PATCH 01/20] Fixes https://github.com/CakeDC/tags/issues/84 hard coded join table model name --- Model/Behavior/TaggableBehavior.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Model/Behavior/TaggableBehavior.php b/Model/Behavior/TaggableBehavior.php index 23f39dd..d3cf40d 100644 --- a/Model/Behavior/TaggableBehavior.php +++ b/Model/Behavior/TaggableBehavior.php @@ -96,7 +96,8 @@ public function bindTagAssociations(Model $model) { 'associationForeignKey' => $associationForeignKey, 'unique' => true, 'conditions' => array( - 'Tagged.model' => $model->name), + $taggedAlias . '.model' => $model->name + ), 'fields' => '', 'dependent' => true, 'with' => $withModel From a3c527abe6af98bc08ddc2edb06e26c18f4e596b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Tue, 8 Jul 2014 01:24:22 +0200 Subject: [PATCH 02/20] Updating README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index dd6b8e1..c3ba3c5 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,14 @@ Tags Plugin for CakePHP ======================= -The **Tags** plugin includes the TaggableBehavior that allows you to simply tag everything. - -It saves all tags in a single tags table and connects any kind of records to them through the tagged table. - [![Bake Status](https://secure.travis-ci.org/CakeDC/tags.png?branch=master)](http://travis-ci.org/CakeDC/tags) [![Downloads](https://poser.pugx.org/CakeDC/tags/d/total.png)](https://packagist.org/packages/CakeDC/tags) [![Latest Version](https://poser.pugx.org/CakeDC/tags/v/stable.png)](https://packagist.org/packages/CakeDC/tags) +The **Tags** plugin includes the TaggableBehavior that allows you to simply tag everything. + +It saves all tags in a single tags table and connects any kind of records to them through the tagged table. + Requirements ------------ From 815f2a41a3c73930376bb197d4cd71d0d2cac1b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Thu, 10 Jul 2014 14:34:59 +0200 Subject: [PATCH 03/20] Update Installation.md --- Docs/Documentation/Installation.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Docs/Documentation/Installation.md b/Docs/Documentation/Installation.md index ea978b8..1898428 100644 --- a/Docs/Documentation/Installation.md +++ b/Docs/Documentation/Installation.md @@ -3,6 +3,12 @@ Installation To install the plugin, place the files in a directory labelled "Tags/" in your "app/Plugin/" directory. +Then, include the following line in your `app/Config/bootstrap.php` to load the plugin in your application. + +``` +CakePlugin::load('Tags'); +``` + Git Submodule ------------- From f598cea6f384cec66f03929b4d2c18047708f92c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Sun, 17 Aug 2014 23:42:42 +0200 Subject: [PATCH 04/20] Refs https://github.com/CakeDC/tags/issues/86 Updating the Configuration.md with missing parameters --- Docs/Documentation/Configuration.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Docs/Documentation/Configuration.md b/Docs/Documentation/Configuration.md index 496f7c3..ab551f0 100644 --- a/Docs/Documentation/Configuration.md +++ b/Docs/Documentation/Configuration.md @@ -42,14 +42,18 @@ public $actsAs = array( The configuration above contains the default values for each setting. Here are some explanations: * **separator:** String used to separate tags in the Model.tags value. (Default: ```,```) -* **field:** Name of the Model field containing the tag list. (Default: ```tags```) * **tagAlias:** Alias for the HABTM relationship between your Model and Tag. (Default: ```Tag```) * **tagClass**: Name of the model representing Tags. (Default: ```Tags.Tag```) * **taggedClass:** Name of the HABTM join model. (Default: ```Tags.Tagged```) +* **field:** Name of the Model field containing the tag list. (Default: ```tags```) * **foreignKey:** Name of the HABTM join model field containing the foreign key to the Tagable model. (Default: ```foreign_key```) * **associationForeignKey:** Name of the HABTM join model field containing the foreign key to the Tag model. (Default: ```tag_id```) * **automaticTagging:** Whether or not the behavior will automatically call saveTags() after each save. (Default: ```true```) +* **language:** Only tags in a certain language, string or array. +* **taggedCounter:** True to update the number of times a particular tag was used for a specific record. * **unsetInAfterFind:** Whether or not the related Tag entries have to be unset after a find. If this value is true, the ```$data['Tag']``` array will be unset and tags will only be available using the ```$data['Model']['tags']``` value. (Default: false) * **resetBinding:** Value passed as the second param of to the ```bindModel()``` call when creating the HABTM association. If set to true, the binding will last only one query. (Default: false) +* **deleteTagsOnEmptyField:** Delete associated Tags if field is empty or empty string. +* **resetBinding:** Reset the `bindModel()` calls, default is false. Note that the ```tagClass```, ```taggedClass```, ```foreignKey``` and ```associationForeignKey``` values must not be changed if you use the plugin as it is shipped. Use these settings when you want to use your own models / tables structure. From 9fea3816e40b9ac310376dbedfd89dd07ccbd5a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Sun, 17 Aug 2014 23:43:48 +0200 Subject: [PATCH 05/20] Refs https://github.com/CakeDC/tags/issues/86 Adding unit tests for Github issue #86 (Deleting more than one tag at time), some CS fixes as well --- Model/Behavior/TaggableBehavior.php | 19 +++++-- .../Model/Behavior/TaggableBehaviorTest.php | 57 ++++++++++++++++++- Test/Case/Model/TagTest.php | 20 ++++--- 3 files changed, 82 insertions(+), 14 deletions(-) diff --git a/Model/Behavior/TaggableBehavior.php b/Model/Behavior/TaggableBehavior.php index d3cf40d..32976ef 100644 --- a/Model/Behavior/TaggableBehavior.php +++ b/Model/Behavior/TaggableBehavior.php @@ -40,6 +40,7 @@ class TaggableBehavior extends ModelBehavior { * taggedCounter - true to update the number of times a particular tag was used for a specific record * unsetInAfterFind - unset 'Tag' results in afterFind * deleteTagsOnEmptyField - delete associated Tags if field is empty. + * resetBinding - reset the bindModel() calls, default is false. * * @var array */ @@ -386,9 +387,13 @@ public function tagArrayToString(Model $model, $data = null) { * @return void */ public function afterSave(Model $model, $created, $options = array()) { - $hasTags = !empty($model->data[$model->alias][$this->settings[$model->alias]['field']]); - if ($this->settings[$model->alias]['automaticTagging'] == true && $hasTags) { - $this->saveTags($model, $model->data[$model->alias][$this->settings[$model->alias]['field']], $model->id); + if (!isset($model->data[$model->alias][$this->settings[$model->alias]['field']])) { + return; + } + $field = $model->data[$model->alias][$this->settings[$model->alias]['field']]; + $hasTags = !empty($field); + if ($this->settings[$model->alias]['automaticTagging'] === true && $hasTags) { + $this->saveTags($model, $field, $model->id); } else if (!$hasTags && $this->settings[$model->alias]['deleteTagsOnEmptyField']) { $this->deleteTagged($model); } @@ -398,15 +403,19 @@ public function afterSave(Model $model, $created, $options = array()) { * Delete associated Tags if record has no tags and deleteTagsOnEmptyField is true * * @param Model $model Model instance + * @param mixed $id Foreign key of the model, string for UUID or integer * @return void */ - public function deleteTagged(Model $model) { + public function deleteTagged(Model $model, $id = null) { extract($this->settings[$model->alias]); $tagModel = $model->{$tagAlias}; + if (is_null($id)) { + $id = $model->id; + } $tagModel->{$taggedAlias}->deleteAll( array( $taggedAlias . '.model' => $model->name, - $taggedAlias . '.foreign_key' => $model->id, + $taggedAlias . '.foreign_key' => $id, ) ); } diff --git a/Test/Case/Model/Behavior/TaggableBehaviorTest.php b/Test/Case/Model/Behavior/TaggableBehaviorTest.php index a3bdf07..9ac5df7 100644 --- a/Test/Case/Model/Behavior/TaggableBehaviorTest.php +++ b/Test/Case/Model/Behavior/TaggableBehaviorTest.php @@ -91,7 +91,8 @@ class TaggableBehaviorTest extends CakeTestCase { public $fixtures = array( 'plugin.tags.tagged', 'plugin.tags.tag', - 'plugin.tags.article'); + 'plugin.tags.article' + ); /** * Method executed before each test @@ -384,6 +385,60 @@ public function testSavingTagsWithDifferentIdentifier() { $this->Article->save($data); $data = $this->Article->findById('article-1'); $this->assertEquals('bar:cakephp, foo:cakephp', $data['Article']['tags']); + } +/** + * testDeletingMoreThanOneTagAtATime + * + * @link https://github.com/CakeDC/tags/issues/86 + * @return void + */ + public function testDeletingMoreThanOneTagAtATime() { + // Adding five tags for testing + $data = array( + 'Article' => array( + 'id' => 'article-test-delete-tags', + 'tags' => 'foo, bar, test, second, third', + ) + ); + $this->Article->create(); + $this->Article->save($data, false); + $result = $this->Article->find('first', array( + 'conditions' => array( + 'id' => 'article-test-delete-tags' + ) + )); + $this->assertEquals($result['Article']['tags'], 'third, second, test, bar, foo'); + // Removing three of the five previously added tags + $result['Article']['tags'] = 'third, second'; + $this->Article->save($result, false); + $result = $this->Article->find('first', array( + 'conditions' => array( + 'id' => 'article-test-delete-tags' + ) + )); + $this->assertEquals($result['Article']['tags'], 'second, third'); + // Removing all tags, empty string - WON'T work as expected because of deleteTagsOnEmptyField + $result['Article']['tags'] = ''; + $this->Article->save($result, false); + $result = $this->Article->find('first', array( + 'conditions' => array( + 'id' => 'article-test-delete-tags' + ) + )); + $this->assertEquals($result['Article']['tags'], 'third, second'); + // Now with deleteTagsOnEmptyField + $this->Article->Behaviors->load('Tags.Taggable', array( + 'deleteTagsOnEmptyField' => true + )); + $result['Article']['tags'] = ''; + $this->Article->save($result, false); + $result = $this->Article->find('first', array( + 'conditions' => array( + 'id' => 'article-test-delete-tags' + ) + )); + $this->assertEquals($result['Article']['tags'], ''); } + } diff --git a/Test/Case/Model/TagTest.php b/Test/Case/Model/TagTest.php index 042c4e3..a37775c 100644 --- a/Test/Case/Model/TagTest.php +++ b/Test/Case/Model/TagTest.php @@ -76,14 +76,14 @@ public function testTagFind() { $expected = array( 'Tag' => array( - 'id' => 'tag-1', - 'identifier' => null, - 'name' => 'CakePHP', - 'keyname' => 'cakephp', + 'id' => 'tag-1', + 'identifier' => null, + 'name' => 'CakePHP', + 'keyname' => 'cakephp', 'occurrence' => 1, 'article_occurrence' => 1, - 'created' => '2008-06-02 18:18:11', - 'modified' => '2008-06-02 18:18:37')); + 'created' => '2008-06-02 18:18:11', + 'modified' => '2008-06-02 18:18:37')); $this->assertEquals($results, $expected); } @@ -114,7 +114,9 @@ public function testAdd() { $result = $this->Tag->find('all', array( 'recursive' => -1, 'fields' => array( - 'Tag.name'))); + 'Tag.name' + ) + )); $result = Set::extract($result, '{n}.Tag.name'); $this->assertTrue(in_array('tag1', $result)); $this->assertTrue(in_array('tag2', $result)); @@ -123,7 +125,9 @@ public function testAdd() { // adding same tags again. $result = $this->Tag->add( array('Tag' => array( - 'tags' => 'tag1, tag2, tag3'))); + 'tags' => 'tag1, tag2, tag3' + ) + )); $this->assertTrue($result); } From 0de35a4b355f236549ddec3a99c4cef3d7689c73 Mon Sep 17 00:00:00 2001 From: Falk Romano Date: Wed, 1 Oct 2014 16:51:08 +0200 Subject: [PATCH 06/20] working on adding possibility to set query string for url --- Test/Case/View/Helper/TagCloudHelperTest.php | 18 ++++++++++++++++++ View/Helper/TagCloudHelper.php | 12 +++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Test/Case/View/Helper/TagCloudHelperTest.php b/Test/Case/View/Helper/TagCloudHelperTest.php index 7786c16..d309b50 100644 --- a/Test/Case/View/Helper/TagCloudHelperTest.php +++ b/Test/Case/View/Helper/TagCloudHelperTest.php @@ -130,6 +130,24 @@ public function testDisplayShouldDefineCorrectSizeWhenCustomWeightField() { $this->assertEquals($result, $expected); } + public function testQueryStringUrlParams() { + $tags = $this->sampleTags; + $tags[0]['Tag']['custom_weight'] = 6; + $tags[1]['Tag']['custom_weight'] = 3; + + $options = array( + 'before' => '', + 'shuffle' => false, + 'extract' => '{n}.Tag.custom_weight', + 'paramType' => 'querystring' + ); + + $result = $this->TagCloud->display($tags, $options); + $expected = 'CakePHP '. + 'CakeDC '; + $this->assertEquals($result, $expected); + } + /** * (non-PHPdoc) * @see cake/tests/lib/CakeTestCase#tearDown($method) diff --git a/View/Helper/TagCloudHelper.php b/View/Helper/TagCloudHelper.php index 5af8daa..2d91d3f 100644 --- a/View/Helper/TagCloudHelper.php +++ b/View/Helper/TagCloudHelper.php @@ -56,7 +56,8 @@ public function display($tags = null, $options = array()) { 'url' => array( 'controller' => 'search' ), - 'named' => 'by' + 'named' => 'by', + 'paramType' => 'named' ); $options = array_merge($defaults, $options); @@ -98,7 +99,12 @@ public function display($tags = null, $options = array()) { * @return array|string */ protected function _tagUrl($tag, $options) { - $options['url'][$options['named']] = $tag[$options['tagModel']]['keyname']; + if ($options['paramType'] === 'named') { + $options['url'][$options['named']] = $tag[$options['tagModel']]['keyname']; + } else { + $options['url']['?'][$options['named']] = $tag[$options['tagModel']]['keyname']; + } + return $options['url']; } @@ -108,7 +114,7 @@ protected function _tagUrl($tag, $options) { * @param string * @param float * @return string - */ + */ protected function _replace($string, $size) { return str_replace("%size%", $size, $string); } From 322fff5a795c2282c3773438b60aa74cc36d2868 Mon Sep 17 00:00:00 2001 From: Falk Romano Date: Wed, 1 Oct 2014 16:56:57 +0200 Subject: [PATCH 07/20] get test passed for query string param --- Test/Case/View/Helper/TagCloudHelperTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Test/Case/View/Helper/TagCloudHelperTest.php b/Test/Case/View/Helper/TagCloudHelperTest.php index d309b50..5dd95df 100644 --- a/Test/Case/View/Helper/TagCloudHelperTest.php +++ b/Test/Case/View/Helper/TagCloudHelperTest.php @@ -130,25 +130,27 @@ public function testDisplayShouldDefineCorrectSizeWhenCustomWeightField() { $this->assertEquals($result, $expected); } +/** + * Test query string param type + */ public function testQueryStringUrlParams() { $tags = $this->sampleTags; $tags[0]['Tag']['custom_weight'] = 6; $tags[1]['Tag']['custom_weight'] = 3; $options = array( - 'before' => '', 'shuffle' => false, 'extract' => '{n}.Tag.custom_weight', 'paramType' => 'querystring' ); $result = $this->TagCloud->display($tags, $options); - $expected = 'CakePHP '. - 'CakeDC '; + $expected = 'CakePHP '. + 'CakeDC '; $this->assertEquals($result, $expected); } -/** + /** * (non-PHPdoc) * @see cake/tests/lib/CakeTestCase#tearDown($method) */ From 14097b6a1b9839b06050edb40ed7610da7af2daf Mon Sep 17 00:00:00 2001 From: Falk Date: Tue, 7 Oct 2014 12:56:51 +0200 Subject: [PATCH 08/20] cs type fixed --- View/Helper/TagCloudHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/View/Helper/TagCloudHelper.php b/View/Helper/TagCloudHelper.php index 2d91d3f..f62dab3 100644 --- a/View/Helper/TagCloudHelper.php +++ b/View/Helper/TagCloudHelper.php @@ -114,7 +114,7 @@ protected function _tagUrl($tag, $options) { * @param string * @param float * @return string - */ + */ protected function _replace($string, $size) { return str_replace("%size%", $size, $string); } From f74b39e2fe47d9de08365eb2f85c9edba3ca2b13 Mon Sep 17 00:00:00 2001 From: Falk Date: Tue, 7 Oct 2014 12:57:21 +0200 Subject: [PATCH 09/20] cs typo fixed --- Test/Case/View/Helper/TagCloudHelperTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Test/Case/View/Helper/TagCloudHelperTest.php b/Test/Case/View/Helper/TagCloudHelperTest.php index 5dd95df..7a4f471 100644 --- a/Test/Case/View/Helper/TagCloudHelperTest.php +++ b/Test/Case/View/Helper/TagCloudHelperTest.php @@ -150,7 +150,7 @@ public function testQueryStringUrlParams() { $this->assertEquals($result, $expected); } - /** +/** * (non-PHPdoc) * @see cake/tests/lib/CakeTestCase#tearDown($method) */ From 8cb4e0b17e7dcd4cfbde92bc1fe1e9cc4d27b361 Mon Sep 17 00:00:00 2001 From: Chris Burke Date: Thu, 9 Oct 2014 21:50:39 -0500 Subject: [PATCH 10/20] Sync behavior settings with documentation --- Docs/Documentation/Configuration.md | 12 ++++++++---- Model/Behavior/TaggableBehavior.php | 11 ++++++----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Docs/Documentation/Configuration.md b/Docs/Documentation/Configuration.md index ab551f0..3868309 100644 --- a/Docs/Documentation/Configuration.md +++ b/Docs/Documentation/Configuration.md @@ -25,16 +25,20 @@ After adding the TaggableBehavior to your model, you will need to update your vi ```php public $actsAs = array( 'Tags.Taggable' => array( - 'separator' => '', - 'field' => 'tags', + 'separator' => ',', 'tagAlias' => 'Tag', 'tagClass' => 'Tags.Tag', + 'taggedAlias' => 'Tagged', 'taggedClass' => 'Tags.Tagged', + 'field' => 'tags', 'foreignKey' => 'foreign_key', 'associationForeignKey' => 'tag_id', + 'cacheOccurrence' => true, 'automaticTagging' => true, + 'taggedCounter' => false, 'unsetInAfterFind' => false, 'resetBinding' => false, + 'deleteTagsOnEmptyField' => false, ) ); ``` @@ -44,16 +48,16 @@ The configuration above contains the default values for each setting. Here are s * **separator:** String used to separate tags in the Model.tags value. (Default: ```,```) * **tagAlias:** Alias for the HABTM relationship between your Model and Tag. (Default: ```Tag```) * **tagClass**: Name of the model representing Tags. (Default: ```Tags.Tag```) +* **taggedAlias:** Alias for the HABTM join model. (Default: ```Tagged```) * **taggedClass:** Name of the HABTM join model. (Default: ```Tags.Tagged```) * **field:** Name of the Model field containing the tag list. (Default: ```tags```) * **foreignKey:** Name of the HABTM join model field containing the foreign key to the Tagable model. (Default: ```foreign_key```) * **associationForeignKey:** Name of the HABTM join model field containing the foreign key to the Tag model. (Default: ```tag_id```) +* **cacheOccurrence:** Name of the HABTM join model field containing the foreign key to the Tag model. (Default: ```true```) * **automaticTagging:** Whether or not the behavior will automatically call saveTags() after each save. (Default: ```true```) -* **language:** Only tags in a certain language, string or array. * **taggedCounter:** True to update the number of times a particular tag was used for a specific record. * **unsetInAfterFind:** Whether or not the related Tag entries have to be unset after a find. If this value is true, the ```$data['Tag']``` array will be unset and tags will only be available using the ```$data['Model']['tags']``` value. (Default: false) * **resetBinding:** Value passed as the second param of to the ```bindModel()``` call when creating the HABTM association. If set to true, the binding will last only one query. (Default: false) * **deleteTagsOnEmptyField:** Delete associated Tags if field is empty or empty string. -* **resetBinding:** Reset the `bindModel()` calls, default is false. Note that the ```tagClass```, ```taggedClass```, ```foreignKey``` and ```associationForeignKey``` values must not be changed if you use the plugin as it is shipped. Use these settings when you want to use your own models / tables structure. diff --git a/Model/Behavior/TaggableBehavior.php b/Model/Behavior/TaggableBehavior.php index 32976ef..9a09a9c 100644 --- a/Model/Behavior/TaggableBehavior.php +++ b/Model/Behavior/TaggableBehavior.php @@ -29,18 +29,19 @@ class TaggableBehavior extends ModelBehavior { * Default settings * * separator - separator used to enter a lot of tags, comma by default + * field - the fieldname that contains the raw tags as string * tagAlias - model alias for Tag model * tagClass - class name of the table storing the tags + * taggedAlias - model alias for the HABTM join model * taggedClass - class name of the HABTM association table between tags and models - * field - the fieldname that contains the raw tags as string * foreignKey - foreignKey used in the HABTM association * associationForeignKey - associationForeignKey used in the HABTM association + * cacheOccurrence - cache the weight or occurence of a tag in the tags table * automaticTagging - if set to true you don't need to use saveTags() manually - * language - only tags in a certain language, string or array * taggedCounter - true to update the number of times a particular tag was used for a specific record * unsetInAfterFind - unset 'Tag' results in afterFind - * deleteTagsOnEmptyField - delete associated Tags if field is empty. * resetBinding - reset the bindModel() calls, default is false. + * deleteTagsOnEmptyField - delete associated Tags if field is empty. * * @var array */ @@ -55,10 +56,10 @@ class TaggableBehavior extends ModelBehavior { 'associationForeignKey' => 'tag_id', 'cacheOccurrence' => true, 'automaticTagging' => true, + 'taggedCounter' => false, 'unsetInAfterFind' => false, 'resetBinding' => false, - 'taggedCounter' => false, - 'deleteTagsOnEmptyField' => false + 'deleteTagsOnEmptyField' => false, ); /** From 05733ad3722c11bf02ec269571fcab95e384dbbc Mon Sep 17 00:00:00 2001 From: Chris Burke Date: Thu, 9 Oct 2014 22:58:18 -0500 Subject: [PATCH 11/20] Add TagCloudHelper paramType setting to documentation --- Docs/Documentation/The-Tag-Cloud-Helper.md | 5 +++-- View/Helper/TagCloudHelper.php | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Docs/Documentation/The-Tag-Cloud-Helper.md b/Docs/Documentation/The-Tag-Cloud-Helper.md index eb5d668..38ca055 100644 --- a/Docs/Documentation/The-Tag-Cloud-Helper.md +++ b/Docs/Documentation/The-Tag-Cloud-Helper.md @@ -44,5 +44,6 @@ The second param for the ```display()``` method is an array of options. The avai * **after:** string to be displayed after each generated link. %size% will be replaced with tag size calculated from the weight. (Default: ```empty```) * **maxSize:** size of the heaviest tag. (Default: ```160```) * **minSize:** size of the lightest tag. (Default: ```80```) -* **url:** an array containing the default url. (Default: ```array('controller' => 'search'```)) -* **named**: the named parameter used to send the tag keyname. (Default: ```by```) \ No newline at end of file +* **url:** an array containing the default URL. (Default: ```array('controller' => 'search'```)) +* **named**: the named parameter used to send the tag keyname. (Default: ```by```) +* **paramType**: the type of URL parameters used (```named``` or ```querystring```). (Default: ```named```) diff --git a/View/Helper/TagCloudHelper.php b/View/Helper/TagCloudHelper.php index f62dab3..478e73c 100644 --- a/View/Helper/TagCloudHelper.php +++ b/View/Helper/TagCloudHelper.php @@ -37,8 +37,9 @@ class TagCloudHelper extends AppHelper { * - after: string to be displayed after each generated link. "%size%" will be replaced with tag size calculated from the weight [default: empty] * - maxSize: size of the heaviest tag [default: 160] * - minSize: size of the lightest tag [default: 80] - * - url: an array containing the default url + * - url: an array containing the default URL * - named: the named parameter used to send the tag [default: by] + * - paramType: the type of URL parameters used (named or querystring) [default: named] * @return string */ public function display($tags = null, $options = array()) { From 81a80d28c78aab9bfd68d188064575bd587f4075 Mon Sep 17 00:00:00 2001 From: Chris Burke Date: Sun, 12 Oct 2014 18:34:26 -0500 Subject: [PATCH 12/20] CS fixes --- .../Migration/001_initialize_tags_schema.php | 49 +++------- .../002_create_times_tagged_field.php | 24 +---- .../003_occurrence_fields_for_tags.php | 24 +---- Config/Migration/map.php | 10 ++- Config/Schema/schema.php | 20 ----- Controller/TagsController.php | 14 +-- Model/Behavior/TaggableBehavior.php | 89 +++++++++---------- Model/Tag.php | 17 ++-- Model/Tagged.php | 28 +++--- Model/TagsAppModel.php | 9 +- Test/Case/Controller/TagsControllerTest.php | 35 +++++--- .../Model/Behavior/TaggableBehaviorTest.php | 50 ++++++++--- Test/Case/Model/TagTest.php | 28 ++++-- Test/Case/Model/TaggedTest.php | 53 ++++++++--- Test/Case/View/Helper/TagCloudHelperTest.php | 39 +++++--- Test/Fixture/ArticleFixture.php | 6 +- Test/Fixture/TagFixture.php | 6 +- Test/Fixture/UserFixture.php | 15 ++-- View/Helper/TagCloudHelper.php | 10 +-- 19 files changed, 273 insertions(+), 253 deletions(-) diff --git a/Config/Migration/001_initialize_tags_schema.php b/Config/Migration/001_initialize_tags_schema.php index f1f0d8b..5ca746a 100644 --- a/Config/Migration/001_initialize_tags_schema.php +++ b/Config/Migration/001_initialize_tags_schema.php @@ -42,13 +42,13 @@ class M49ac311a54844a9d87o822502jedc423 extends CakeMigration { 'up' => array( 'create_table' => array( 'tagged' => array( - 'id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36, 'key' => 'primary'), - 'foreign_key' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36), - 'tag_id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36), - 'model' => array('type' => 'string', 'null' => false, 'default' => NULL, 'key' => 'index'), - 'language' => array('type' => 'string', 'null' => true, 'default' => NULL, 'length' => 6), - 'created' => array('type' => 'datetime', 'null' => true, 'default' => NULL), - 'modified' => array('type' => 'datetime', 'null' => true, 'default' => NULL), + 'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'primary'), + 'foreign_key' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36), + 'tag_id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36), + 'model' => array('type' => 'string', 'null' => false, 'default' => null, 'key' => 'index'), + 'language' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 6), + 'created' => array('type' => 'datetime', 'null' => true, 'default' => null), + 'modified' => array('type' => 'datetime', 'null' => true, 'default' => null), 'indexes' => array( 'PRIMARY' => array('column' => 'id', 'unique' => 1), 'UNIQUE_TAGGING' => array('column' => array('model', 'foreign_key', 'tag_id', 'language'), 'unique' => 1), @@ -57,13 +57,13 @@ class M49ac311a54844a9d87o822502jedc423 extends CakeMigration { ) ), 'tags' => array( - 'id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36, 'key' => 'primary'), - 'identifier' => array('type' => 'string', 'null' => true, 'default' => NULL, 'length' => 30, 'key' => 'index'), - 'name' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 30), - 'keyname' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 30), + 'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'primary'), + 'identifier' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 30, 'key' => 'index'), + 'name' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 30), + 'keyname' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 30), 'weight' => array('type' => 'integer', 'null' => false, 'default' => 0, 'length' => 2), - 'created' => array('type' => 'datetime', 'null' => true, 'default' => NULL), - 'modified' => array('type' => 'datetime', 'null' => true, 'default' => NULL), + 'created' => array('type' => 'datetime', 'null' => true, 'default' => null), + 'modified' => array('type' => 'datetime', 'null' => true, 'default' => null), 'indexes' => array( 'PRIMARY' => array('column' => 'id', 'unique' => 1), 'UNIQUE_TAG' => array('column' => array('identifier', 'keyname'), 'unique' => 1) @@ -76,27 +76,4 @@ class M49ac311a54844a9d87o822502jedc423 extends CakeMigration { ) ); -/** - * Before migration callback - * - * @param string $direction, up or down direction of migration process - * @return boolean Should process continue - * @access public - */ - public function before($direction) { - return true; - } - -/** - * After migration callback - * - * @param string $direction, up or down direction of migration process - * @return boolean Should process continue - * @access public - */ - public function after($direction) { - return true; - } - } -?> \ No newline at end of file diff --git a/Config/Migration/002_create_times_tagged_field.php b/Config/Migration/002_create_times_tagged_field.php index bf1914f..1bcf9c6 100644 --- a/Config/Migration/002_create_times_tagged_field.php +++ b/Config/Migration/002_create_times_tagged_field.php @@ -42,8 +42,8 @@ class M4c0d42bcd12c4db099c105f40e8f3d6d extends CakeMigration { 'create_field' => array( 'tagged' => array( 'times_tagged' => array('type' => 'integer', 'null' => false, 'default' => 1), - ) ), + ), ), 'down' => array( 'drop_field' => array( @@ -52,26 +52,4 @@ class M4c0d42bcd12c4db099c105f40e8f3d6d extends CakeMigration { ), ); -/** - * Before migration callback - * - * @param string $direction, up or down direction of migration process - * @return boolean Should process continue - * @access public - */ - public function before($direction) { - return true; - } - -/** - * After migration callback - * - * @param string $direction, up or down direction of migration process - * @return boolean Should process continue - * @access public - */ - public function after($direction) { - return true; - } } -?> \ No newline at end of file diff --git a/Config/Migration/003_occurrence_fields_for_tags.php b/Config/Migration/003_occurrence_fields_for_tags.php index 1b46878..a1e9a6e 100644 --- a/Config/Migration/003_occurrence_fields_for_tags.php +++ b/Config/Migration/003_occurrence_fields_for_tags.php @@ -42,8 +42,8 @@ class M8d01880f01c11e0be500800200c9a66 extends CakeMigration { 'create_field' => array( 'tags' => array( 'occurrence' => array('type' => 'integer', 'null' => false, 'default' => 0, 'length' => 8), - ) ), + ), 'drop_field' => array( 'tags' => array('weight') ), @@ -60,26 +60,4 @@ class M8d01880f01c11e0be500800200c9a66 extends CakeMigration { ), ); -/** - * Before migration callback - * - * @param string $direction, up or down direction of migration process - * @return boolean Should process continue - * @access public - */ - public function before($direction) { - return true; - } - -/** - * After migration callback - * - * @param string $direction, up or down direction of migration process - * @return boolean Should process continue - * @access public - */ - public function after($direction) { - return true; - } } -?> \ No newline at end of file diff --git a/Config/Migration/map.php b/Config/Migration/map.php index 1439cc8..f279118 100644 --- a/Config/Migration/map.php +++ b/Config/Migration/map.php @@ -17,10 +17,12 @@ $map = array( 1 => array( - '001_initialize_tags_schema' => 'M49ac311a54844a9d87o822502jedc423'), + '001_initialize_tags_schema' => 'M49ac311a54844a9d87o822502jedc423', + ), 2 => array( - '002_create_times_tagged_field' => 'M4c0d42bcd12c4db099c105f40e8f3d6d'), + '002_create_times_tagged_field' => 'M4c0d42bcd12c4db099c105f40e8f3d6d', + ), 3 => array( - '003_occurrence_fields_for_tags' => 'M8d01880f01c11e0be500800200c9a66'), + '003_occurrence_fields_for_tags' => 'M8d01880f01c11e0be500800200c9a66', + ), ); -?> \ No newline at end of file diff --git a/Config/Schema/schema.php b/Config/Schema/schema.php index 2d04602..88388ee 100644 --- a/Config/Schema/schema.php +++ b/Config/Schema/schema.php @@ -11,26 +11,6 @@ class TagSchema extends CakeSchema { -/** - * Before callback - * - * @param array Event - * @return boolean - */ - public function before($event = array()) { - return true; - } - -/** - * After callback - * - * @param array Event - * @return boolean - */ - public function after($event = array()) { - return true; - } - /** * Schema for taggeds table * diff --git a/Controller/TagsController.php b/Controller/TagsController.php index 4414043..2fafa1c 100644 --- a/Controller/TagsController.php +++ b/Controller/TagsController.php @@ -59,7 +59,7 @@ public function index() { /** * View * - * @param string + * @param string $keyName Tag key name * @return void */ public function view($keyName = null) { @@ -77,14 +77,14 @@ public function view($keyName = null) { * @return void */ public function admin_index() { - $this->{$this->modelClass}->recursive = 0; + $this->{$this->modelClass}->recursive = 0; $this->set('tags', $this->Paginator->paginate()); } /** * Views a single tag * - * @param string tag UUID + * @param string $keyName Tag key name * @return void */ public function admin_view($keyName) { @@ -113,7 +113,7 @@ public function admin_add() { /** * Edits a tag * - * @param string tag UUID + * @param string $tagId Tag UUID * @return void */ public function admin_edit($tagId = null) { @@ -138,11 +138,11 @@ public function admin_edit($tagId = null) { /** * Deletes a tag * - * @param string tag UUID + * @param string $tagId Tag UUID * @return void */ - public function admin_delete($id = null) { - if ($this->{$this->modelClass}->delete($id)) { + public function admin_delete($tagId = null) { + if ($this->{$this->modelClass}->delete($tagId)) { $this->Session->setFlash(__d('tags', 'Tag deleted.')); } else { $this->Session->setFlash(__d('tags', 'Invalid Tag.')); diff --git a/Model/Behavior/TaggableBehavior.php b/Model/Behavior/TaggableBehavior.php index 9a09a9c..dc2b4a3 100644 --- a/Model/Behavior/TaggableBehavior.php +++ b/Model/Behavior/TaggableBehavior.php @@ -28,20 +28,20 @@ class TaggableBehavior extends ModelBehavior { /** * Default settings * - * separator - separator used to enter a lot of tags, comma by default - * field - the fieldname that contains the raw tags as string - * tagAlias - model alias for Tag model - * tagClass - class name of the table storing the tags - * taggedAlias - model alias for the HABTM join model - * taggedClass - class name of the HABTM association table between tags and models - * foreignKey - foreignKey used in the HABTM association - * associationForeignKey - associationForeignKey used in the HABTM association - * cacheOccurrence - cache the weight or occurence of a tag in the tags table - * automaticTagging - if set to true you don't need to use saveTags() manually - * taggedCounter - true to update the number of times a particular tag was used for a specific record - * unsetInAfterFind - unset 'Tag' results in afterFind - * resetBinding - reset the bindModel() calls, default is false. - * deleteTagsOnEmptyField - delete associated Tags if field is empty. + * separator - separator used to enter a lot of tags, comma by default + * field - the fieldname that contains the raw tags as string + * tagAlias - model alias for Tag model + * tagClass - class name of the table storing the tags + * taggedAlias - model alias for the HABTM join model + * taggedClass - class name of the HABTM association table between tags and models + * foreignKey - foreignKey used in the HABTM association + * associationForeignKey - associationForeignKey used in the HABTM association + * cacheOccurrence - cache the weight or occurence of a tag in the tags table + * automaticTagging - if set to true you don't need to use saveTags() manually + * taggedCounter - true to update the number of times a particular tag was used for a specific record + * unsetInAfterFind - unset 'Tag' results in afterFind + * resetBinding - reset the bindModel() calls, default is false + * deleteTagsOnEmptyField - delete associated Tags if field is empty * * @var array */ @@ -65,10 +65,8 @@ class TaggableBehavior extends ModelBehavior { /** * Setup * - * @param Model $model - * @param array $config - * - * @internal param array $settings + * @param Model $model Model instance that behavior is attached to + * @param array $config Configuration settings from model * @return void */ public function setup(Model $model, $config = array()) { @@ -82,9 +80,9 @@ public function setup(Model $model, $config = array()) { } /** - * bindTagAssociations + * Bind tag assocations * - * @param Model $model + * @param Model $model Model instance that behavior is attached to * @return void */ public function bindTagAssociations(Model $model) { @@ -151,14 +149,14 @@ public function disassembleTags(Model $model, $string = '', $separator = ',') { /** * Saves a string of tags * - * @param Model $model + * @param Model $model Model instance that behavior is attached to * @param string $string comma separeted list of tags to be saved - * Tags can contain special tokens called `identifiers´ to namespace tags or classify them into catageories. - * A valid string is "foo, bar, cakephp:special". The token `cakephp´ will end up as the identifier or category for the tag `special´ + * Tags can contain special tokens called `identifiers´ to namespace tags or classify them into catageories. + * A valid string is "foo, bar, cakephp:special". The token `cakephp´ will end up as the identifier or category for the tag `special´ * @param mixed $foreignKey the identifier for the record to associate the tags with - * @param boolean $update true will remove tags that are not in the $string, false wont - * do this and just add new tags without removing existing tags associated to - * the current set foreign key + * @param bool $update True will remove tags that are not in the $string, false won't + * do this and just add new tags without removing existing tags associated to + * the current set foreign key * @return array */ public function saveTags(Model $model, $string = null, $foreignKey = null, $update = true) { @@ -295,8 +293,8 @@ public function saveTags(Model $model, $string = null, $foreignKey = null, $upda /** * Cache the weight or occurence of a tag in the tags table * - * @param Model $model instance of a model - * @param int|string|array $tagIds + * @param Model $model Model instance that behavior is attached to + * @param int|string|array $tagIds Tag ID or list of tag IDs * @return void */ public function cacheOccurrence(Model $model, $tagIds) { @@ -326,23 +324,23 @@ public function cacheOccurrence(Model $model, $tagIds) { )); $tagModel->save($data, array( 'validate' => false, - 'callbacks' => false) - ); + 'callbacks' => false, + )); } } /** * Creates a multibyte safe unique key * - * @param Model $model - * @param string Tag name string - * @returns string Multibyte safe key string + * @param Model $model Model instance that behavior is attached to + * @param string $string Tag name string + * @return string Multibyte safe key string */ public function multibyteKey(Model $model, $string = null) { $str = mb_strtolower($string); $str = preg_replace('/\xE3\x80\x80/', ' ', $str); $str = str_replace(array('_', '-'), '', $str); - $str = preg_replace( '#[:\#\*"()~$^{}`@+=;,<>!&%\.\]\/\'\\\\|\[]#', "\x20", $str ); + $str = preg_replace('#[:\#\*"()~$^{}`@+=;,<>!&%\.\]\/\'\\\\|\[]#', "\x20", $str); $str = str_replace('?', '', $str); $str = trim($str); $str = preg_replace('#\x20+#', '', $str); @@ -354,14 +352,15 @@ public function multibyteKey(Model $model, $string = null) { * initialization of data for text input * * Example usage (only 'Tag.name' field is needed inside of method): + * * * $this->Blog->hasAndBelongsToMany['Tag']['fields'] = array('name', 'keyname'); * $blog = $this->Blog->read(null, 123); * $blog['Blog']['tags'] = $this->Blog->Tag->tagArrayToString($blog['Tag']); * * - * @param Model $model - * @param array $data + * @param Model $model Model instance that behavior is attached to + * @param array $data Tag data array * @return string */ public function tagArrayToString(Model $model, $data = null) { @@ -371,7 +370,7 @@ public function tagArrayToString(Model $model, $data = null) { if (!empty($tag['identifier'])) { $tags[] = $tag['identifier'] . ':' . $tag['name']; } else { - $tags[] = $tag['name']; + $tags[] = $tag['name']; } } return join($this->settings[$model->alias]['separator'] . ' ', $tags); @@ -382,9 +381,9 @@ public function tagArrayToString(Model $model, $data = null) { /** * afterSave callback * - * @param Model $model - * @param array $created - * @param array $options + * @param Model $model Model instance that behavior is attached to + * @param bool $created True if this save created a new record + * @param array $options Options passed from Model::save() * @return void */ public function afterSave(Model $model, $created, $options = array()) { @@ -395,7 +394,7 @@ public function afterSave(Model $model, $created, $options = array()) { $hasTags = !empty($field); if ($this->settings[$model->alias]['automaticTagging'] === true && $hasTags) { $this->saveTags($model, $field, $model->id); - } else if (!$hasTags && $this->settings[$model->alias]['deleteTagsOnEmptyField']) { + } elseif (!$hasTags && $this->settings[$model->alias]['deleteTagsOnEmptyField']) { $this->deleteTagged($model); } } @@ -403,7 +402,7 @@ public function afterSave(Model $model, $created, $options = array()) { /** * Delete associated Tags if record has no tags and deleteTagsOnEmptyField is true * - * @param Model $model Model instance + * @param Model $model Model instance that behavior is attached to * @param mixed $id Foreign key of the model, string for UUID or integer * @return void */ @@ -424,9 +423,9 @@ public function deleteTagged(Model $model, $id = null) { /** * afterFind Callback * - * @param Model $model - * @param array $results - * @param boolean $primary + * @param Model $model Model instance that behavior is attached to + * @param mixed $results The results of the find operation + * @param bool $primary Whether this model is being queried directly (vs. being queried as an association) * @return array */ public function afterFind(Model $model, $results, $primary = false) { diff --git a/Model/Tag.php b/Model/Tag.php index 80d9921..7195ff3 100644 --- a/Model/Tag.php +++ b/Model/Tag.php @@ -50,14 +50,16 @@ class Tag extends TagsAppModel { /** * Returns the data for a single tag * + * @param string $keyName Tag key name * @throws CakeException - * @param string keyname * @return array */ public function view($keyName = null) { $result = $this->find('first', array( 'conditions' => array( - $this->alias . '.keyname' => $keyName))); + $this->alias . '.keyname' => $keyName + ) + )); if (empty($result)) { throw new CakeException(__d('tags', 'Invalid Tag.')); @@ -68,8 +70,8 @@ public function view($keyName = null) { /** * Pre-populates the tag table with entered tags * - * @param array post data, should be Contoller->data - * @return boolean + * @param array $postData Post data, should be Contoller->data + * @return bool */ public function add($postData = null) { if (isset($postData[$this->alias]['tags'])) { @@ -88,16 +90,17 @@ public function add($postData = null) { /** * Edits an existing tag, allows only to modify upper/lowercased characters * + * @param string $tagId Tag UUID + * @param array $postData Controller post data usually $this->request->data * @throws CakeException - * @param string tag uuid - * @param array controller post data usually $this->request->data * @return mixed True on successfully save else post data as array */ public function edit($tagId = null, $postData = null) { $tag = $this->find('first', array( 'contain' => array(), 'conditions' => array( - $this->alias . '.' . $this->primaryKey => $tagId) + $this->alias . '.' . $this->primaryKey => $tagId + ) )); $this->set($tag); diff --git a/Model/Tagged.php b/Model/Tagged.php index 50f2b2d..4f0ae38 100644 --- a/Model/Tagged.php +++ b/Model/Tagged.php @@ -50,14 +50,14 @@ class Tagged extends TagsAppModel { * Returns a tag cloud * * The result contains a "weight" field which has a normalized size of the tag - * occurrence set. The min and max size can be set by passing 'minSize" and - * 'maxSize' to the query. This value can be used in the view to controll the + * occurrence set. The min and max size can be set by passing 'minSize' and + * 'maxSize' to the query. This value can be used in the view to control the * size of the tag font. * + * @param string $state State string ('before' or 'after') + * @param array $query Query array + * @param array $results Result set for 'after' state * @todo Ideas to improve this are welcome - * @param string $state - * @param array $query - * @param array $results * @return array * @link https://github.com/CakeDC/tags/issues/10 */ @@ -135,16 +135,18 @@ public function _findCloud($state, $query, $results = array()) { * Find all the Model entries tagged with a given tag * * The query must contain a Model name, and can contain a 'by' key with the Tag keyname to filter the results + * * * $this->Article->Tagged->find('tagged', array( * 'by' => 'cakephp', * 'model' => 'Article')); - * * - * @TODO Find a way to populate the "magic" field Article.tags - * @param string $state - * @param array $query - * @param array $results + * @param string $state State string ('before' or 'after') + * @param array $query Query array + * @param array $results Result set for 'after' state + * @todo Find a way to populate the "magic" field Article.tags + * @throws InvalidArgumentException if recursive setting is -1 * @return mixed Query array if state is before, array of results or integer (count) if state is after */ public function _findTagged($state, $query, $results = array()) { @@ -157,7 +159,11 @@ public function _findTagged($state, $query, $results = array()) { 'foreignKey' => 'foreign_key', 'type' => 'INNER', 'conditions' => array( - $this->alias . '.model' => $Model->alias)))), false); + $this->alias . '.model' => $Model->alias + ) + ) + ) + ), false); if (!isset($query['recursive'])) { $query['recursive'] = 0; diff --git a/Model/TagsAppModel.php b/Model/TagsAppModel.php index 2c1a738..783b6e6 100644 --- a/Model/TagsAppModel.php +++ b/Model/TagsAppModel.php @@ -23,14 +23,15 @@ class TagsAppModel extends AppModel { * @var array */ public $actsAs = array( - 'Containable'); + 'Containable' + ); /** * Customized paginateCount method * - * @param array - * @param integer - * @param array + * @param array $conditions Query conditions + * @param int $recursive Recursive setting + * @param array $extra Extra settings * @return array */ public function paginateCount($conditions = array(), $recursive = 0, $extra = array()) { diff --git a/Test/Case/Controller/TagsControllerTest.php b/Test/Case/Controller/TagsControllerTest.php index 117bb55..d3ab2d2 100644 --- a/Test/Case/Controller/TagsControllerTest.php +++ b/Test/Case/Controller/TagsControllerTest.php @@ -23,7 +23,7 @@ class TestTagsController extends TagsController { /** * Auto render * - * @var boolean + * @var bool */ public $autoRender = false; @@ -37,6 +37,9 @@ class TestTagsController extends TagsController { /** * Override controller method for testing * + * @param string|array $url URL to redirect to (copied to `$this->redirectUrl`) + * @param int $status Optional HTTP status code (eg: 404) + * @param bool $exit If true, exit() will be called after the redirect * @return void */ public function redirect($url, $status = null, $exit = true) { @@ -46,11 +49,14 @@ public function redirect($url, $status = null, $exit = true) { /** * Override controller method for testing * + * @param string $view View to use for rendering + * @param string $layout Layout to use * @return void */ - public function render($action = null, $layout = null, $file = null) { - $this->renderedView = $action; + public function render($view = null, $layout = null) { + $this->renderedView = $view; } + } /** @@ -68,7 +74,8 @@ class TagsControllerTest extends CakeTestCase { */ public $fixtures = array( 'plugin.tags.tagged', - 'plugin.tags.tag'); + 'plugin.tags.tag' + ); /** * Tags Controller Instance @@ -87,7 +94,8 @@ public function setUp() { $this->Tags = new TestTagsController(new CakeRequest(null, false)); $this->Tags->params = array( 'named' => array(), - 'url' => array()); + 'url' => array() + ); $this->Tags->constructClasses(); $this->Tags->Session = $this->getMock('SessionComponent', array(), array(), '', false); } @@ -175,7 +183,6 @@ public function testAdminDelete() { ->with($this->equalTo(__d('tags', 'Tag deleted.'))) ->will($this->returnValue(true)); - $this->Tags->admin_delete('WRONG-ID!!!'); $this->assertEquals($this->Tags->redirectUrl, array('action' => 'index')); @@ -191,14 +198,18 @@ public function testAdminDelete() { public function testAdminAdd() { $this->Tags->data = array( 'Tag' => array( - 'tags' => 'tag1, tag2, tag3')); + 'tags' => 'tag1, tag2, tag3' + ) + ); $this->Tags->admin_add(); $this->assertEquals($this->Tags->redirectUrl, array('action' => 'index')); // adding same tags again. $this->Tags->data = array( 'Tag' => array( - 'tags' => 'tag1, tag2, tag3')); + 'tags' => 'tag1, tag2, tag3' + ) + ); $this->Tags->admin_add(); $this->assertEquals($this->Tags->redirectUrl, array('action' => 'index')); } @@ -219,14 +230,18 @@ public function testAdminEdit() { 'occurrence' => 1, 'article_occurrence' => 1, 'created' => '2008-06-02 18:18:11', - 'modified' => '2008-06-02 18:18:37')); + 'modified' => '2008-06-02 18:18:37' + ) + ); $this->assertEquals($this->Tags->data, $tag); $this->Tags->data = array( 'Tag' => array( 'id' => 'tag-1', - 'name' => 'CAKEPHP')); + 'name' => 'CAKEPHP' + ) + ); $this->Tags->admin_edit('tag-1'); $this->assertEquals($this->Tags->redirectUrl, array('action' => 'index')); diff --git a/Test/Case/Model/Behavior/TaggableBehaviorTest.php b/Test/Case/Model/Behavior/TaggableBehaviorTest.php index 9ac5df7..3fe4905 100644 --- a/Test/Case/Model/Behavior/TaggableBehaviorTest.php +++ b/Test/Case/Model/Behavior/TaggableBehaviorTest.php @@ -126,7 +126,9 @@ public function testOccurrenceCache() { $resultBefore = $this->Article->Tag->find('first', array( 'contain' => array(), 'conditions' => array( - 'Tag.keyname' => 'cakephp'))); + 'Tag.keyname' => 'cakephp' + ) + )); // adding a new record with the cakephp tag to increase the occurrence $data = array('title' => 'Test Article', 'tags' => 'cakephp, php'); @@ -136,7 +138,9 @@ public function testOccurrenceCache() { $resultAfter = $this->Article->Tag->find('first', array( 'contain' => array(), 'conditions' => array( - 'Tag.keyname' => 'cakephp'))); + 'Tag.keyname' => 'cakephp' + ) + )); $this->assertEquals($resultAfter['Tag']['occurrence'] - $resultBefore['Tag']['occurrence'], 1); @@ -146,7 +150,9 @@ public function testOccurrenceCache() { $resultAfter = $this->Article->Tag->find('first', array( 'contain' => array(), 'conditions' => array( - 'Tag.keyname' => 'cakephp'))); + 'Tag.keyname' => 'cakephp' + ) + )); $this->assertEquals($resultAfter['Tag']['occurrence'], 1); } @@ -161,7 +167,9 @@ public function testTagSaving() { $this->Article->save($data, false); $result = $this->Article->find('first', array( 'conditions' => array( - 'id' => 'article-1'))); + 'id' => 'article-1' + ) + )); $this->assertTrue(!empty($result['Article']['tags'])); $data['tags'] = 'foo, developer, developer, php'; @@ -169,23 +177,27 @@ public function testTagSaving() { $result = $this->Article->find('first', array( 'contain' => array('Tag'), 'conditions' => array( - 'id' => 'article-1'))); + 'id' => 'article-1' + ) + )); $this->assertTrue(!empty($result['Article']['tags'])); $this->assertEquals(3, count($result['Tag'])); - $data['tags'] = 'cakephp:foo, developer, cakephp:developer, cakephp:php'; $this->Article->save($data, false); $result = $this->Article->Tag->find('all', array( 'recursive' => -1, 'order' => 'Tag.identifier DESC, Tag.name ASC', 'conditions' => array( - 'Tag.identifier' => 'cakephp'))); + 'Tag.identifier' => 'cakephp' + ) + )); $result = Set::extract($result, '{n}.Tag.keyname'); $this->assertEquals($result, array( - 'developer', 'foo', 'php')); + 'developer', 'foo', 'php' + )); $this->assertFalse($this->Article->saveTags('foo, bar', null)); $this->assertFalse($this->Article->saveTags(array('foo', 'bar'), 'something')); @@ -227,7 +239,9 @@ public function testTagArrayToString() { $this->Article->save($data, false); $result = $this->Article->find('first', array( 'conditions' => array( - 'id' => 'article-1'))); + 'id' => 'article-1' + ) + )); $result = $this->Article->tagArrayToString($result['Tag']); $this->assertTrue(!empty($result)); $this->assertInternalType('string', $result); @@ -241,7 +255,9 @@ public function testTagArrayToString() { $this->Article->save($data, false); $result = $this->Article->find('first', array( 'conditions' => array( - 'id' => 'article-1'))); + 'id' => 'article-1' + ) + )); $result = $this->Article->tagArrayToString($result['Tag']); $this->assertTrue(!empty($result)); @@ -274,13 +290,17 @@ public function testAfterFind() { $result = $this->Article->find('first', array( 'conditions' => array( - 'id' => 'article-1'))); + 'id' => 'article-1' + ) + )); $this->assertTrue(isset($result['Tag'])); $this->Article->Behaviors->Taggable->settings['Article']['unsetInAfterFind'] = true; $result = $this->Article->find('first', array( 'conditions' => array( - 'id' => 'article-1'))); + 'id' => 'article-1' + ) + )); $this->assertTrue(!isset($result['Tag'])); } @@ -293,7 +313,8 @@ public function testAfterFindFields() { $this->Article->Behaviors->detach('Taggable'); $results = $this->Article->find('first', array( 'recursive' => -1, - 'fields' => array('id'))); + 'fields' => array('id') + )); $expected = array($this->Article->alias => array('id' => 'article-1')); $this->assertIdentical($results, $expected); } @@ -362,7 +383,8 @@ public function testSavingTagsDoesNotCreateEmptyRecords() { $this->Article->save($data, false); $result = $this->Article->find('first', array( 'conditions' => array( - 'id' => 'article-1') + 'id' => 'article-1' + ) )); $count = $this->Article->Tag->find('count', array( diff --git a/Test/Case/Model/TagTest.php b/Test/Case/Model/TagTest.php index a37775c..43ca44f 100644 --- a/Test/Case/Model/TagTest.php +++ b/Test/Case/Model/TagTest.php @@ -83,7 +83,9 @@ public function testTagFind() { 'occurrence' => 1, 'article_occurrence' => 1, 'created' => '2008-06-02 18:18:11', - 'modified' => '2008-06-02 18:18:37')); + 'modified' => '2008-06-02 18:18:37' + ) + ); $this->assertEquals($results, $expected); } @@ -107,9 +109,11 @@ public function testView() { * @return void */ public function testAdd() { - $result = $this->Tag->add( - array('Tag' => array( - 'tags' => 'tag1, tag2, tag3'))); + $result = $this->Tag->add(array( + 'Tag' => array( + 'tags' => 'tag1, tag2, tag3' + ) + )); $this->assertTrue($result); $result = $this->Tag->find('all', array( 'recursive' => -1, @@ -123,8 +127,8 @@ public function testAdd() { $this->assertTrue(in_array('tag3', $result)); // adding same tags again. - $result = $this->Tag->add( - array('Tag' => array( + $result = $this->Tag->add(array( + 'Tag' => array( 'tags' => 'tag1, tag2, tag3' ) )); @@ -143,20 +147,26 @@ public function testEdit() { $data = array( 'Tag' => array( 'id' => 'tag-1', - 'name' => 'CAKEPHP')); + 'name' => 'CAKEPHP' + ) + ); $this->assertTrue($this->Tag->edit('tag-1', $data)); $data = array( 'Tag' => array( 'id' => 'tag-1', - 'name' => 'CAKEPHP111')); + 'name' => 'CAKEPHP111' + ) + ); $this->assertFalse($this->Tag->edit('tag-1', $data)); $data = array( 'Tag' => array( 'id' => 'tag-1', 'name' => 'CAKEPHP', - 'keyname' => '')); + 'keyname' => '' + ) + ); $this->assertEquals($this->Tag->edit('tag-1', $data), $data); $this->expectException('CakeException'); diff --git a/Test/Case/Model/TaggedTest.php b/Test/Case/Model/TaggedTest.php index b2cae29..b8c35f3 100644 --- a/Test/Case/Model/TaggedTest.php +++ b/Test/Case/Model/TaggedTest.php @@ -105,7 +105,9 @@ public function testTaggedFind() { 'language' => 'eng', 'times_tagged' => 1, 'created' => '2008-12-02 12:32:31', - 'modified' => '2008-12-02 12:32:31')); + 'modified' => '2008-12-02 12:32:31' + ) + ); $this->assertEquals($result, $expected); } @@ -117,7 +119,8 @@ public function testTaggedFind() { */ public function testFindCloud() { $result = $this->Tagged->find('cloud', array( - 'model' => 'Article')); + 'model' => 'Article' + )); $this->assertEquals(count($result), 3); $this->assertTrue(isset($result[0][0]['occurrence'])); @@ -127,7 +130,8 @@ public function testFindCloud() { $this->assertTrue(is_array($result) && !empty($result)); $result = $this->Tagged->find('cloud', array( - 'limit' => 1)); + 'limit' => 1 + )); $this->assertEquals(count($result), 1); } @@ -140,17 +144,20 @@ public function testFindTagged() { $this->Tagged->recursive = -1; $result = $this->Tagged->find('tagged', array( 'by' => 'cakephp', - 'model' => 'Article')); + 'model' => 'Article' + )); $this->assertEquals(count($result), 1); $this->assertEquals($result[0]['Article']['id'], 'article-1'); $result = $this->Tagged->find('tagged', array( - 'model' => 'Article')); + 'model' => 'Article' + )); $this->assertEquals(count($result), 2); // Test call to paginateCount by Controller::pagination() $result = $this->Tagged->paginateCount(array(), 1, array( 'model' => 'Article', - 'type' => 'tagged')); + 'type' => 'tagged' + )); $this->assertEquals($result, 2); } @@ -164,13 +171,15 @@ public function testFindTaggedWithConditions() { $result = $this->Tagged->find('tagged', array( 'by' => 'cakephp', 'model' => 'Article', - 'conditions' => array('Article.title LIKE' => 'Second %'))); + 'conditions' => array('Article.title LIKE' => 'Second %') + )); $this->assertEquals(count($result), 0); $result = $this->Tagged->find('tagged', array( 'by' => 'cakephp', 'model' => 'Article', - 'conditions' => array('Article.title LIKE' => 'First %'))); + 'conditions' => array('Article.title LIKE' => 'First %') + )); $this->assertEquals(count($result), 1); $this->assertEquals($result[0]['Article']['id'], 'article-1'); } @@ -186,16 +195,24 @@ public function testDeepAssociationsHasOne() { 'belongsTo' => array( 'Article' => array( 'className' => 'TaggedArticle', - 'foreignKey' => 'foreign_key')))); + 'foreignKey' => 'foreign_key' + ) + ) + )); $this->Tagged->Article->bindModel(array( 'hasOne' => array( - 'User' => array()))); + 'User' => array() + ) + )); $result = $this->Tagged->find('all', array( 'contain' => array( 'Article' => array( - 'User')))); + 'User' + ) + ) + )); $this->assertEquals($result[0]['Article']['User']['name'], 'CakePHP'); } @@ -211,16 +228,24 @@ public function testDeepAssociationsBelongsTo() { 'belongsTo' => array( 'Article' => array( 'className' => 'TaggedArticle', - 'foreignKey' => 'foreign_key')))); + 'foreignKey' => 'foreign_key' + ) + ) + )); $this->Tagged->Article->bindModel(array( 'belongsTo' => array( - 'User' => array()))); + 'User' => array() + ) + )); $result = $this->Tagged->find('all', array( 'contain' => array( 'Article' => array( - 'User')))); + 'User' + ) + ) + )); $this->assertEquals($result[0]['Article']['User']['name'], 'CakePHP'); } diff --git a/Test/Case/View/Helper/TagCloudHelperTest.php b/Test/Case/View/Helper/TagCloudHelperTest.php index 7a4f471..bd8e73d 100644 --- a/Test/Case/View/Helper/TagCloudHelperTest.php +++ b/Test/Case/View/Helper/TagCloudHelperTest.php @@ -35,7 +35,9 @@ class TagCloudHelperTest extends CakeTestCase { 'keyname' => 'cakephp', 'weight' => 2, 'created' => '2008-06-02 18:18:11', - 'modified' => '2008-06-02 18:18:37')), + 'modified' => '2008-06-02 18:18:37' + ) + ), array( 'Tag' => array( 'id' => 2, @@ -44,7 +46,9 @@ class TagCloudHelperTest extends CakeTestCase { 'keyname' => 'cakedc', 'weight' => 2, 'created' => '2008-06-01 18:18:15', - 'modified' => '2008-06-01 18:18:15')), + 'modified' => '2008-06-01 18:18:15' + ) + ), ); /** @@ -55,8 +59,9 @@ class TagCloudHelperTest extends CakeTestCase { public $TagCloud; /** - * (non-PHPdoc) - * @see cake/tests/lib/CakeTestCase#setUp($method) + * Setup the test case + * + * @return void */ public function setUp() { parent::setUp(); @@ -76,7 +81,8 @@ public function testDisplay() { // Test tags shuffling $options = array( - 'shuffle' => true); + 'shuffle' => true + ); $expected = 'CakePHP CakeDC '; $i = 100; do { @@ -87,7 +93,8 @@ public function testDisplay() { // Test normal display $options = array( - 'shuffle' => false); + 'shuffle' => false + ); $result = $this->TagCloud->display($this->sampleTags, $options); $this->assertEquals($result, $expected); @@ -101,18 +108,23 @@ public function testDisplay() { 'named' => 'query' )); $result = $this->TagCloud->display($this->sampleTags, $options); - $expected = 'CakePHP '. + $expected = 'CakePHP ' . 'CakeDC '; $this->assertEquals($result, $expected); $tags = $this->sampleTags; $tags[1]['Tag']['weight'] = 1; $result = $this->TagCloud->display($tags, $options); - $expected = 'CakePHP '. + $expected = 'CakePHP ' . 'CakeDC '; $this->assertEquals($result, $expected); } +/** + * Test that display method defines correct size when using custom weight fields + * + * @return void + */ public function testDisplayShouldDefineCorrectSizeWhenCustomWeightField() { $tags = $this->sampleTags; $tags[0]['Tag']['custom_weight'] = 6; @@ -125,13 +137,15 @@ public function testDisplayShouldDefineCorrectSizeWhenCustomWeightField() { ); $result = $this->TagCloud->display($tags, $options); - $expected = 'CakePHP '. + $expected = 'CakePHP ' . 'CakeDC '; $this->assertEquals($result, $expected); } /** * Test query string param type + * + * @return void */ public function testQueryStringUrlParams() { $tags = $this->sampleTags; @@ -145,14 +159,15 @@ public function testQueryStringUrlParams() { ); $result = $this->TagCloud->display($tags, $options); - $expected = 'CakePHP '. + $expected = 'CakePHP ' . 'CakeDC '; $this->assertEquals($result, $expected); } /** - * (non-PHPdoc) - * @see cake/tests/lib/CakeTestCase#tearDown($method) + * Tear down the test case + * + * @return void */ public function tearDown() { parent::tearDown(); diff --git a/Test/Fixture/ArticleFixture.php b/Test/Fixture/ArticleFixture.php index 2edfe6d..c7a094d 100644 --- a/Test/Fixture/ArticleFixture.php +++ b/Test/Fixture/ArticleFixture.php @@ -37,11 +37,13 @@ class ArticleFixture extends CakeTestFixture { array( 'id' => 'article-1', 'title' => 'First Article', - 'user_id' => 'user-1'), + 'user_id' => 'user-1' + ), array( 'id' => 'article-2', 'title' => 'Second Article', - 'user_id' => 'user-2'), + 'user_id' => 'user-2' + ), array( 'id' => 'article-3', 'title' => 'Third Article', diff --git a/Test/Fixture/TagFixture.php b/Test/Fixture/TagFixture.php index 75159b4..132154f 100644 --- a/Test/Fixture/TagFixture.php +++ b/Test/Fixture/TagFixture.php @@ -58,7 +58,8 @@ class TagFixture extends CakeTestFixture { 'occurrence' => 1, 'article_occurrence' => 1, 'created' => '2008-06-02 18:18:11', - 'modified' => '2008-06-02 18:18:37'), + 'modified' => '2008-06-02 18:18:37' + ), array( 'id' => 'tag-2', 'identifier' => null, @@ -67,7 +68,8 @@ class TagFixture extends CakeTestFixture { 'occurrence' => 1, 'article_occurrence' => 1, 'created' => '2008-06-01 18:18:15', - 'modified' => '2008-06-01 18:18:15'), + 'modified' => '2008-06-01 18:18:15' + ), array( 'id' => 'tag-3', 'identifier' => null, diff --git a/Test/Fixture/UserFixture.php b/Test/Fixture/UserFixture.php index 386c26b..f225186 100644 --- a/Test/Fixture/UserFixture.php +++ b/Test/Fixture/UserFixture.php @@ -23,9 +23,10 @@ class UserFixture extends CakeTestFixture { * @var array */ public $fields = array( - 'id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36), + 'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36), 'name' => array('type' => 'string', 'null' => false), - 'article_id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36)); + 'article_id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36) + ); /** * records property @@ -36,14 +37,18 @@ class UserFixture extends CakeTestFixture { array( 'id' => 'user-1', 'name' => 'CakePHP', - 'article_id' => 'article-1'), + 'article_id' => 'article-1' + ), array( 'id' => 'user-2', 'name' => 'Second User', - 'article_id' => 'article-2'), + 'article_id' => 'article-2' + ), array( 'id' => 'user-3', 'name' => 'Third User', - 'article_id' => 'article-3')); + 'article_id' => 'article-3' + ) + ); } diff --git a/View/Helper/TagCloudHelper.php b/View/Helper/TagCloudHelper.php index 478e73c..c5caf04 100644 --- a/View/Helper/TagCloudHelper.php +++ b/View/Helper/TagCloudHelper.php @@ -29,7 +29,7 @@ class TagCloudHelper extends AppHelper { /** * Method to output a tag-cloud formatted based on the weight of the tags * - * @param array $tags + * @param array $tags Tag data array * @param array $options Display options. Valid keys are: * - shuffle: true to shuffle the tag list, false to display them in the same order than passed [default: true] * - extract: Set::extract() compatible format string. Path to extract weight values from the $tags array [default: {n}.Tag.weight] @@ -95,8 +95,8 @@ public function display($tags = null, $options = array()) { /** * Generates the URL for a tag * - * @param array - * @param array + * @param array $tag Tag data array + * @param array $options Display options * @return array|string */ protected function _tagUrl($tag, $options) { @@ -112,8 +112,8 @@ protected function _tagUrl($tag, $options) { /** * Replaces %size% in strings with the calculated "size" of the tag * - * @param string - * @param float + * @param string $string Before or after string + * @param float $size Calculated size * @return string */ protected function _replace($string, $size) { From 2c7f1b036e4034ac6c768c8441232210a093d97d Mon Sep 17 00:00:00 2001 From: Chris Burke Date: Tue, 14 Oct 2014 01:48:09 -0500 Subject: [PATCH 13/20] Add test for custom aliases and fix errors where alias has been hardcoded --- Model/Behavior/TaggableBehavior.php | 34 +++--- .../Model/Behavior/TaggableBehaviorTest.php | 102 ++++++++++++++++-- 2 files changed, 112 insertions(+), 24 deletions(-) diff --git a/Model/Behavior/TaggableBehavior.php b/Model/Behavior/TaggableBehavior.php index dc2b4a3..5ec7280 100644 --- a/Model/Behavior/TaggableBehavior.php +++ b/Model/Behavior/TaggableBehavior.php @@ -88,6 +88,7 @@ public function setup(Model $model, $config = array()) { public function bindTagAssociations(Model $model) { extract($this->settings[$model->alias]); + $withClass = substr($withModel, strrpos($withModel, '.') + 1); $model->bindModel(array( 'hasAndBelongsToMany' => array( $tagAlias => array( @@ -96,7 +97,7 @@ public function bindTagAssociations(Model $model) { 'associationForeignKey' => $associationForeignKey, 'unique' => true, 'conditions' => array( - $taggedAlias . '.model' => $model->name + $withClass . '.model' => $model->name ), 'fields' => '', 'dependent' => true, @@ -226,7 +227,7 @@ public function saveTags(Model $model, $string = null, $foreignKey = null, $upda $taggedAlias . '.foreign_key' => $foreignKey, $taggedAlias . '.language' => Configure::read('Config.language'), $taggedAlias . '.tag_id' => $existingTagIds), - 'fields' => 'Tagged.tag_id' + 'fields' => $taggedAlias . '.tag_id' )); $deleteAll = array( @@ -248,13 +249,16 @@ public function saveTags(Model $model, $string = null, $foreignKey = null, $upda $taggedAlias . '.model' => $model->name, $taggedAlias . '.foreign_key' => $foreignKey, $taggedAlias . '.language' => Configure::read('Config.language')), - 'fields' => 'Tagged.tag_id' + 'fields' => $taggedAlias . '.tag_id' )); - $oldTagIds = Set::extract($oldTagIds, '/Tagged/tag_id'); + $oldTagIds = Set::extract($oldTagIds, '/' . $taggedAlias . '/tag_id'); $tagModel->{$taggedAlias}->deleteAll($deleteAll, false); } elseif ($this->settings[$model->alias]['taggedCounter'] && !empty($alreadyTagged)) { - $tagModel->{$taggedAlias}->updateAll(array('times_tagged' => 'times_tagged + 1'), array('Tagged.tag_id' => $alreadyTagged)); + $tagModel->{$taggedAlias}->updateAll( + array('times_tagged' => 'times_tagged + 1'), + array($taggedAlias . '.tag_id' => $alreadyTagged) + ); } foreach ($existingTagIds as $tagId) { @@ -274,11 +278,11 @@ public function saveTags(Model $model, $string = null, $foreignKey = null, $upda $taggedAlias . '.model' => $model->name, $taggedAlias . '.foreign_key' => $foreignKey, $taggedAlias . '.language' => Configure::read('Config.language')), - 'fields' => 'Tagged.tag_id' + 'fields' => $taggedAlias . '.tag_id' )); if (!empty($newTagIds)) { - $newTagIds = Set::extract($newTagIds, '{n}.Tagged.tag_id'); + $newTagIds = Set::extract($newTagIds, '{n}.' . $taggedAlias . '.tag_id'); } $this->cacheOccurrence($model, array_merge($oldTagIds, $newTagIds)); @@ -302,24 +306,28 @@ public function cacheOccurrence(Model $model, $tagIds) { $tagIds = array($tagIds); } + $tagAlias = $this->settings[$model->alias]['tagAlias']; + $taggedAlias = $this->settings[$model->alias]['taggedAlias']; + + $tagModel = $model->{$tagAlias}; + $taggedModel = $tagModel->{$taggedAlias}; + + $fieldName = Inflector::underscore($model->name) . '_occurrence'; foreach ($tagIds as $tagId) { - $fieldName = Inflector::underscore($model->name) . '_occurrence'; - $tagModel = $model->{$this->settings[$model->alias]['tagAlias']}; - $taggedModel = $tagModel->{$this->settings[$model->alias]['taggedAlias']}; $data = array($tagModel->primaryKey => $tagId); if ($tagModel->hasField($fieldName)) { $data[$fieldName] = $taggedModel->find('count', array( 'conditions' => array( - 'Tagged.tag_id' => $tagId, - 'Tagged.model' => $model->name + $taggedAlias . '.tag_id' => $tagId, + $taggedAlias . '.model' => $model->name ) )); } $data['occurrence'] = $taggedModel->find('count', array( 'conditions' => array( - 'Tagged.tag_id' => $tagId + $taggedAlias . '.tag_id' => $tagId ) )); $tagModel->save($data, array( diff --git a/Test/Case/Model/Behavior/TaggableBehaviorTest.php b/Test/Case/Model/Behavior/TaggableBehaviorTest.php index 3fe4905..cc33304 100644 --- a/Test/Case/Model/Behavior/TaggableBehaviorTest.php +++ b/Test/Case/Model/Behavior/TaggableBehaviorTest.php @@ -60,6 +60,40 @@ class Article extends CakeTestModel { public $actsAs = array('Tags.Taggable'); } +/** + * Custom tag model + * + * @package tags + * @subpackage tags.tests.cases.behaviors + */ +class CustomTag extends CakeTestModel { + +/** + * Use table + * + * @var string + */ + public $useTable = 'tags'; + +} + +/** + * Custom tagged model + * + * @package tags + * @subpackage tags.tests.cases.behaviors + */ +class CustomTagged extends CakeTestModel { + +/** + * Use table + * + * @var string + */ + public $useTable = 'tagged'; + +} + /** * TaggableTest * @@ -94,6 +128,48 @@ class TaggableBehaviorTest extends CakeTestCase { 'plugin.tags.article' ); +/** + * Behavior settings. + * + * @var array + */ + public $behaviorSettings = array(); + +/** + * Default behavior settings. + * + * @var array + */ + public $defaultBehaviorSettings = array(); + +/** + * Custom behavior settings. + * + * @var array + */ + public $customBehaviorSettings = array( + 'tagAlias' => 'CustomTagAlias', + 'taggedAlias' => 'CustomTaggedAlias', + 'tagClass' => 'Tags.CustomTag', + 'taggedClass' => 'Tags.CustomTagged', + ); + +/** + * Run test cases multiple times with different behavior settings + * + * @param PHPUnit_Framework_TestResult $result The test result object + * @return PHPUnit_Framework_TestResult + */ + public function run(PHPUnit_Framework_TestResult $result = null) { + $this->behaviorSettings = $this->defaultBehaviorSettings; + $result = parent::run($result); + + $this->behaviorSettings = $this->customBehaviorSettings; + $result = parent::run($result); + + return $result; + } + /** * Method executed before each test * @@ -103,7 +179,7 @@ public function setUp() { parent::setUp(); $this->Article = ClassRegistry::init('Article'); Configure::write('Config.language', 'eng'); - $this->Article->Behaviors->attach('Tags.Taggable', array()); + $this->Article->Behaviors->attach('Tags.Taggable', $this->behaviorSettings); } /** @@ -123,10 +199,12 @@ public function tearDown() { * @return void */ public function testOccurrenceCache() { - $resultBefore = $this->Article->Tag->find('first', array( + $tagAlias = $this->Article->Behaviors->Taggable->settings['Article']['tagAlias']; + + $resultBefore = $this->Article->{$tagAlias}->find('first', array( 'contain' => array(), 'conditions' => array( - 'Tag.keyname' => 'cakephp' + $tagAlias . '.keyname' => 'cakephp' ) )); @@ -135,25 +213,25 @@ public function testOccurrenceCache() { $this->Article->create(); $this->Article->save($data, false); - $resultAfter = $this->Article->Tag->find('first', array( + $resultAfter = $this->Article->{$tagAlias}->find('first', array( 'contain' => array(), 'conditions' => array( - 'Tag.keyname' => 'cakephp' + $tagAlias . '.keyname' => 'cakephp' ) )); - $this->assertEquals($resultAfter['Tag']['occurrence'] - $resultBefore['Tag']['occurrence'], 1); + $this->assertEquals($resultAfter[$tagAlias]['occurrence'] - $resultBefore[$tagAlias]['occurrence'], 1); // updating the record to not have the cakephp tag anymore, decreases the occurrence $data = array('id' => $this->Article->id, 'title' => 'Test Article', 'tags' => 'php, something, else'); $this->Article->save($data, false); - $resultAfter = $this->Article->Tag->find('first', array( + $resultAfter = $this->Article->{$tagAlias}->find('first', array( 'contain' => array(), 'conditions' => array( - 'Tag.keyname' => 'cakephp' + $tagAlias . '.keyname' => 'cakephp' ) )); - $this->assertEquals($resultAfter['Tag']['occurrence'], 1); + $this->assertEquals($resultAfter[$tagAlias]['occurrence'], 1); } /** @@ -284,6 +362,8 @@ public function testMultibyteKey() { * @return void */ public function testAfterFind() { + $tagAlias = $this->Article->Behaviors->Taggable->settings['Article']['tagAlias']; + $data['id'] = 'article-1'; $data['tags'] = 'foo, bar, test'; $this->Article->save($data, false); @@ -293,7 +373,7 @@ public function testAfterFind() { 'id' => 'article-1' ) )); - $this->assertTrue(isset($result['Tag'])); + $this->assertTrue(isset($result[$tagAlias])); $this->Article->Behaviors->Taggable->settings['Article']['unsetInAfterFind'] = true; $result = $this->Article->find('first', array( @@ -301,7 +381,7 @@ public function testAfterFind() { 'id' => 'article-1' ) )); - $this->assertTrue(!isset($result['Tag'])); + $this->assertTrue(!isset($result[$tagAlias])); } /** From fe764e25fc8434f93e8b457dd6890b3a23c21abe Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Wed, 15 Oct 2014 23:16:57 +0400 Subject: [PATCH 14/20] fix issue in case tag was not created --- Model/Behavior/TaggableBehavior.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Model/Behavior/TaggableBehavior.php b/Model/Behavior/TaggableBehavior.php index 5ec7280..6b8b727 100644 --- a/Model/Behavior/TaggableBehavior.php +++ b/Model/Behavior/TaggableBehavior.php @@ -212,8 +212,9 @@ public function saveTags(Model $model, $string = null, $foreignKey = null, $upda } foreach ($newTags as $key => $newTag) { $tagModel->create(); - $tagModel->save($newTag); - $newTagIds[] = $tagModel->getLastInsertId(); + if ($tagModel->save($newTag)) { + $newTagIds[] = $tagModel->getLastInsertId(); + } } if ($foreignKey !== false) { From 83af27f0cff2f3bcd09d2947e57faa504241ac9b Mon Sep 17 00:00:00 2001 From: Chris Burke Date: Thu, 16 Oct 2014 14:47:33 -0500 Subject: [PATCH 15/20] Adding test coverage for no plugin prefix and fixing bug introduced earlier (refs #89) --- Model/Behavior/TaggableBehavior.php | 2 +- .../Model/Behavior/TaggableBehaviorTest.php | 40 ++++++++----------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/Model/Behavior/TaggableBehavior.php b/Model/Behavior/TaggableBehavior.php index 6b8b727..c2fa759 100644 --- a/Model/Behavior/TaggableBehavior.php +++ b/Model/Behavior/TaggableBehavior.php @@ -88,7 +88,7 @@ public function setup(Model $model, $config = array()) { public function bindTagAssociations(Model $model) { extract($this->settings[$model->alias]); - $withClass = substr($withModel, strrpos($withModel, '.') + 1); + list($plugin, $withClass) = pluginSplit($withModel, true); $model->bindModel(array( 'hasAndBelongsToMany' => array( $tagAlias => array( diff --git a/Test/Case/Model/Behavior/TaggableBehaviorTest.php b/Test/Case/Model/Behavior/TaggableBehaviorTest.php index cc33304..1cf72df 100644 --- a/Test/Case/Model/Behavior/TaggableBehaviorTest.php +++ b/Test/Case/Model/Behavior/TaggableBehaviorTest.php @@ -133,25 +133,18 @@ class TaggableBehaviorTest extends CakeTestCase { * * @var array */ - public $behaviorSettings = array(); - -/** - * Default behavior settings. - * - * @var array - */ - public $defaultBehaviorSettings = array(); - -/** - * Custom behavior settings. - * - * @var array - */ - public $customBehaviorSettings = array( - 'tagAlias' => 'CustomTagAlias', - 'taggedAlias' => 'CustomTaggedAlias', - 'tagClass' => 'Tags.CustomTag', - 'taggedClass' => 'Tags.CustomTagged', + public $behaviorSettings = array( + 'default' => array(), + 'noPluginPrefix' => array( + 'tagClass' => 'Tag', + 'taggedClass' => 'Tagged', + ), + 'customAliasAndClass' => array( + 'tagAlias' => 'CustomTagAlias', + 'taggedAlias' => 'CustomTaggedAlias', + 'tagClass' => 'Tags.CustomTag', + 'taggedClass' => 'Tags.CustomTagged', + ), ); /** @@ -161,11 +154,10 @@ class TaggableBehaviorTest extends CakeTestCase { * @return PHPUnit_Framework_TestResult */ public function run(PHPUnit_Framework_TestResult $result = null) { - $this->behaviorSettings = $this->defaultBehaviorSettings; - $result = parent::run($result); - - $this->behaviorSettings = $this->customBehaviorSettings; - $result = parent::run($result); + foreach ($this->behaviorSettings as $behaviorSettings) { + $this->behaviorSettings = $behaviorSettings; + $result = parent::run($result); + } return $result; } From 8c1225cb77b05eaf5060f80b6db92f5c91b26c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20M=2E=20Gonz=C3=A1lez=20Mart=C3=ADn?= Date: Tue, 23 Jun 2015 09:58:42 +0100 Subject: [PATCH 16/20] updating travis to use CakePHP 2.6 --- .travis.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index bc06b36..772fa94 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,22 +12,22 @@ env: - REQUIRE="phpunit/phpunit:3.7.31" matrix: - - DB=mysql CAKE_VERSION=master + - DB=mysql CAKE_VERSION=2.6 matrix: include: - php: 5.3 env: - - CAKE_VERSION=master + - CAKE_VERSION=2.6 - php: 5.4 env: - - CAKE_VERSION=master + - CAKE_VERSION=2.6 - php: 5.5 env: - - CAKE_VERSION=master + - CAKE_VERSION=2.6 before_script: - - git clone https://github.com/burzum/travis.git --depth 1 ../travis + - git clone https://github.com/steinkel/travis.git --depth 1 ../travis - ../travis/before_script.sh - if [ "$PHPCS" != 1 ]; then echo " From 2c53d02117b17847bfd1fc24a38a175cf051acd9 Mon Sep 17 00:00:00 2001 From: Kenshin Okinaka Date: Tue, 14 Jul 2015 16:13:49 +0900 Subject: [PATCH 17/20] updating travis to use CakePHP 2.7 --- .travis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 772fa94..8f6fd38 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,19 +12,19 @@ env: - REQUIRE="phpunit/phpunit:3.7.31" matrix: - - DB=mysql CAKE_VERSION=2.6 + - DB=mysql CAKE_VERSION=2.7 matrix: include: - php: 5.3 env: - - CAKE_VERSION=2.6 + - CAKE_VERSION=2.7 - php: 5.4 env: - - CAKE_VERSION=2.6 + - CAKE_VERSION=2.7 - php: 5.5 env: - - CAKE_VERSION=2.6 + - CAKE_VERSION=2.7 before_script: - git clone https://github.com/steinkel/travis.git --depth 1 ../travis From eb7232c91811a9f21c0f1b69b8480da450609ad2 Mon Sep 17 00:00:00 2001 From: Kenshin Okinaka Date: Tue, 14 Jul 2015 16:22:25 +0900 Subject: [PATCH 18/20] Fixing validations to use CakePHP 2.7 --- Model/Tag.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Model/Tag.php b/Model/Tag.php index 7195ff3..c75857a 100644 --- a/Model/Tag.php +++ b/Model/Tag.php @@ -43,8 +43,8 @@ class Tag extends TagsAppModel { * @var array */ public $validate = array( - 'name' => array('rule' => 'notEmpty'), - 'keyname' => array('rule' => 'notEmpty') + 'name' => array('rule' => 'notBlank'), + 'keyname' => array('rule' => 'notBlank') ); /** From 3034e03acc16c8ae2be7554c66c4c87916c670c2 Mon Sep 17 00:00:00 2001 From: OKINAKA Kenshin Date: Mon, 10 Aug 2015 10:29:31 +0900 Subject: [PATCH 19/20] Adding custom validation for BC. --- Model/Tag.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Model/Tag.php b/Model/Tag.php index c75857a..38baf5e 100644 --- a/Model/Tag.php +++ b/Model/Tag.php @@ -47,6 +47,23 @@ class Tag extends TagsAppModel { 'keyname' => array('rule' => 'notBlank') ); +/** + * Custom validation for keeping BC to CakePHP version below 2.7 + * + * @param array $check + * @return bool + */ + public function notBlank($check) { + $value = array_values($check); + $value = $value[0]; + if (method_exists('Validation', 'notBlank')) { + return Validation::notBlank($value); + } else { + // below 2.7 + return Validation::notEmpty($value); + } + } + /** * Returns the data for a single tag * From 1476dfce430d802886031a5658001fae0890480a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20M=2E=20Gonz=C3=A1lez=20Mart=C3=ADn?= Date: Thu, 22 Oct 2015 14:56:28 +0100 Subject: [PATCH 20/20] keep tests running for 2.6 --- .travis.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.travis.yml b/.travis.yml index 8f6fd38..9a9fdd8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,6 +25,15 @@ matrix: - php: 5.5 env: - CAKE_VERSION=2.7 + - php: 5.3 + env: + - CAKE_VERSION=2.6 + - php: 5.4 + env: + - CAKE_VERSION=2.6 + - php: 5.5 + env: + - CAKE_VERSION=2.6 before_script: - git clone https://github.com/steinkel/travis.git --depth 1 ../travis