Skip to content

Commit

Permalink
Merge pull request #57 from tractorcow/pulls/fix-source
Browse files Browse the repository at this point in the history
BUG Fix items not included from source appearing in suggestions
  • Loading branch information
assertchris committed Nov 3, 2015
2 parents 04e9948 + 75a7e95 commit 641128f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
4 changes: 1 addition & 3 deletions code/TagField.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,11 @@ protected function getTags($term) {
*/
$source = $this->getSource();

$dataClass = $source->dataClass();

$titleField = $this->getTitleField();

$term = Convert::raw2sql($term);

$query = $dataClass::get()
$query = $source
->filter($titleField . ':PartialMatch:nocase', $term)
->sort($titleField)
->limit($this->getLazyLoadItemLimit());
Expand Down
38 changes: 38 additions & 0 deletions tests/TagFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,44 @@ function testItSuggestsTags() {
);
}

/**
* Tests that TagField supports pre-filtered data sources
*/
public function testRestrictedSuggestions() {
$source = TagFieldTestBlogTag::get()->exclude('Title', 'Tag2');
$field = new TagField('Tags', '', $source);

/**
* Partial tag title match.
*/
$request = $this->getNewRequest(array('term' => 'Tag'));

$this->assertEquals(
'{"items":[{"id":1,"text":"Tag1"}]}',
$field->suggest($request)->getBody()
);

/**
* Exact tag title match.
*/
$request = $this->getNewRequest(array('term' => 'Tag1'));

$this->assertEquals(
'{"items":[{"id":1,"text":"Tag1"}]}',
$field->suggest($request)->getBody()
);

/**
* Excluded item doesn't appear in matches
*/
$request = $this->getNewRequest(array('term' => 'Tag2'));

$this->assertEquals(
'{"items":[]}',
$field->suggest($request)->getBody()
);
}

/**
* @param array $parameters
*
Expand Down

0 comments on commit 641128f

Please sign in to comment.