BUG Fix items not included from source appearing in suggestions

Fixes https://github.com/silverstripe/silverstripe-blog/issues/295
This commit is contained in:
Damian Mooyman 2015-11-03 18:16:45 +13:00
parent 04e99483da
commit 75a7e956ba
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
*