Skip to content
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.

Commit

Permalink
Removed Behaviors->detach, replaced Behaviors->attach by Behaviors->l…
Browse files Browse the repository at this point in the history
  • Loading branch information
ravage84 committed Nov 11, 2013
1 parent 62795ba commit 363cdc7
Showing 1 changed file with 19 additions and 37 deletions.
56 changes: 19 additions & 37 deletions Test/Case/Model/Behavior/SearchableBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,9 @@ public function testGetWildcards() {
* @link http://github.com/CakeDC/Search/issues#issue/3
*/
public function testValueCondition() {
$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
array('name' => 'slug', 'type' => 'value'));
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');
$data = array();
$result = $this->Article->parseCriteria($data);
$this->assertEquals(array(), $result);
Expand All @@ -247,57 +246,51 @@ public function testValueCondition() {
$expected = array('Article.slug' => 'first_article');
$this->assertEquals($expected, $result);

$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
array('name' => 'fakeslug', 'type' => 'value', 'field' => 'Article2.slug'));
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');
$data = array('fakeslug' => 'first_article');
$result = $this->Article->parseCriteria($data);
$expected = array('Article2.slug' => 'first_article');
$this->assertEquals($expected, $result);

// Testing http://github.com/CakeDC/Search/issues#issue/3
$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
array('name' => 'views', 'type' => 'value'));
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');
$data = array('views' => '0');
$result = $this->Article->parseCriteria($data);
$this->assertEquals(array('Article.views' => 0), $result);

$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
array('name' => 'views', 'type' => 'value'));
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');
$data = array('views' => 0);
$result = $this->Article->parseCriteria($data);
$this->assertEquals(array('Article.views' => 0), $result);

$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
array('name' => 'views', 'type' => 'value'));
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');
$data = array('views' => '');
$result = $this->Article->parseCriteria($data);
$this->assertEquals(array(), $result);

// multiple fields + cross model searches
$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
'faketitle' => array('type' => 'value', 'field' => array('title', 'User.name'))
);
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');
$data = array('faketitle' => 'First');
$result = $this->Article->parseCriteria($data);
$expected = array('OR' => array('Article.title' => 'First', 'User.name' => 'First'));
$this->assertEquals($expected, $result);

// multiple select dropdown
$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
'fakesource' => array('type' => 'value')
);
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');
$data = array('fakesource' => array(5, 9));
$result = $this->Article->parseCriteria($data);
$expected = array('Article.fakesource' => array(5, 9));
Expand All @@ -310,10 +303,9 @@ public function testValueCondition() {
* @return void
*/
public function testLikeCondition() {
$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
array('name' => 'title', 'type' => 'like'));
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');

$data = array();
$result = $this->Article->parseCriteria($data);
Expand All @@ -324,22 +316,20 @@ public function testLikeCondition() {
$expected = array('Article.title LIKE' => '%First%');
$this->assertEquals($expected, $result);

$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
array('name' => 'faketitle', 'type' => 'like', 'field' => 'Article.title'));
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');

$data = array('faketitle' => 'First');
$result = $this->Article->parseCriteria($data);
$expected = array('Article.title LIKE' => '%First%');
$this->assertEquals($expected, $result);

// wildcards should be treated as normal text
$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
array('name' => 'faketitle', 'type' => 'like', 'field' => 'Article.title')
);
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');
$data = array('faketitle' => '%First_');
$result = $this->Article->parseCriteria($data);
$expected = array('Article.title LIKE' => '%\%First\_%');
Expand Down Expand Up @@ -401,58 +391,53 @@ public function testLikeCondition() {
$this->assertEquals($expected, $result);

// multiple OR fields per field
$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
array('name' => 'faketitle', 'type' => 'like', 'field' => array('title', 'descr'))
);
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');
$data = array('faketitle' => 'First');

$result = $this->Article->parseCriteria($data);
$expected = array('OR' => array('Article.title LIKE' => '%First%', 'Article.descr LIKE' => '%First%'));
$this->assertEquals($expected, $result);

// set before => false dynamically
$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
array('name' => 'faketitle', 'type' => 'like', 'field' => array('title', 'descr'), 'before' => false)
);
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');
$data = array('faketitle' => 'First');
$result = $this->Article->parseCriteria($data);
$expected = array('OR' => array('Article.title LIKE' => 'First%', 'Article.descr LIKE' => 'First%'));
$this->assertEquals($expected, $result);

// manually define the before/after type
$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
array('name' => 'faketitle', 'type' => 'like', 'field' => array('title'), 'before' => '_', 'after' => '_')
);
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');
$data = array('faketitle' => 'First');
$result = $this->Article->parseCriteria($data);
$expected = array('Article.title LIKE' => '_First_');
$this->assertEquals($expected, $result);

// cross model searches + named keys (shorthand)
$this->Article->bindModel(array('belongsTo' => array('User')));
$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
'faketitle' => array('type' => 'like', 'field' => array('title', 'User.name'), 'before' => false, 'after' => true)
);
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');
$data = array('faketitle' => 'First');
$result = $this->Article->parseCriteria($data);
$expected = array('OR' => array('Article.title LIKE' => 'First%', 'User.name LIKE' => 'First%'));
$this->assertEquals($expected, $result);

// with already existing or conditions + named keys (shorthand)
$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
'faketitle' => array('type' => 'like', 'field' => array('title', 'User.name'), 'before' => false, 'after' => true),
'otherfaketitle' => array('type' => 'like', 'field' => array('descr', 'comment'), 'before' => false, 'after' => true)
);
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');

$data = array('faketitle' => 'First', 'otherfaketitle' => 'Second');
$result = $this->Article->parseCriteria($data);
Expand All @@ -463,11 +448,10 @@ public function testLikeCondition() {
$this->assertEquals($expected, $result);

// wildcards and and/or connectors
$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
array('name' => 'faketitle', 'type' => 'like', 'field' => 'Article.title', 'connectorAnd' => '+', 'connectorOr' => ',', 'before' => true, 'after' => true)
);
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');
$data = array('faketitle' => 'First%+Second%, Third%');
$result = $this->Article->parseCriteria($data);
$expected = array(0 => array('OR' => array(
Expand Down Expand Up @@ -529,13 +513,12 @@ public function testSubQueryEmptyCondition() {
$expression = $this->Article->getDatasource()->expression('Article.id in (SELECT `Tagged`.`foreign_key` FROM `' . $database . '`.`' . $this->Article->tablePrefix . 'tagged` AS `Tagged` LEFT JOIN `' . $database . '`.`' . $this->Article->tablePrefix . 'tags` AS `Tag` ON (`Tagged`.`tag_id` = `Tag`.`id`) WHERE `Tag`.`name` = \'Cake\')');
$expected = array($expression);

$this->Article->Behaviors->detach('Searchable');

// new syntax
$this->Article->filterArgs = array(
'tags' => array('type' => 'subquery', 'method' => 'findByTags', 'field' => 'Article.id')
);
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');

$result = $this->Article->parseCriteria($data);
$this->assertEquals($expected, $result);
Expand Down Expand Up @@ -592,7 +575,7 @@ public function testQueryOr2Example() {
* @return void
*/
public function testQueryWithBehaviorCallCondition() {
$this->Article->Behaviors->attach('Filter');
$this->Article->Behaviors->load('Filter');
$this->Article->filterArgs = array(
array('name' => 'filter', 'type' => 'query', 'method' => 'mostFilterConditions'));

Expand Down Expand Up @@ -636,10 +619,9 @@ public function testExpressionCallCondition() {
* @return void
*/
public function testDefaultValue() {
$this->Article->Behaviors->detach('Searchable');
$this->Article->filterArgs = array(
'range' => array('type' => 'expression', 'defaultValue' => '100', 'method' => 'makeRangeCondition', 'field' => 'Article.views BETWEEN ? AND ?'));
$this->Article->Behaviors->attach('Search.Searchable');
$this->Article->Behaviors->load('Search.Searchable');

$data = array();
$result = $this->Article->parseCriteria($data);
Expand Down

0 comments on commit 363cdc7

Please sign in to comment.