Merge pull request #57 from tractorcow/pulls/fix-source

BUG Fix items not included from source appearing in suggestions
This commit is contained in:
Christopher Pitt 2015-11-04 12:11:30 +13:00
commit 641128f2d6
2 changed files with 39 additions and 3 deletions

View File

@ -332,13 +332,11 @@ class TagField extends DropdownField {
*/
$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());

View File

@ -147,6 +147,44 @@ class TagFieldTest extends SapphireTest {
);
}
/**
* 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
*