From 6449e3a1a7babbc1b5969ccb674d852ef842a6a4 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Tue, 19 Oct 2010 04:53:36 +0000 Subject: [PATCH] MINOR Added FulltextSearchable::get_searchable_classes() in order to introspect currently searchable classes, added FulltextSearchableTest, added documentation (from r111789) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112923 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- search/ContentControllerSearchExtension.php | 2 +- search/FulltextSearchable.php | 46 +++++++++++++++++++-- tests/search/FulltextSearchableTest.php | 44 ++++++++++++++++++++ 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 tests/search/FulltextSearchableTest.php diff --git a/search/ContentControllerSearchExtension.php b/search/ContentControllerSearchExtension.php index ab0d9c03d..7cba78ed7 100644 --- a/search/ContentControllerSearchExtension.php +++ b/search/ContentControllerSearchExtension.php @@ -28,7 +28,7 @@ class ContentControllerSearchExtension extends Extension { new FormAction('results', _t('SearchForm.GO', 'Go')) ); $form = new SearchForm($this->owner, 'SearchForm', $fields, $actions); - $form->classesToSearch(FulltextSearchable::$searchableClasses); + $form->classesToSearch(FulltextSearchable::get_searchable_classes()); return $form; } diff --git a/search/FulltextSearchable.php b/search/FulltextSearchable.php index b5b4e9ffc..8b46fa7d1 100644 --- a/search/FulltextSearchable.php +++ b/search/FulltextSearchable.php @@ -1,16 +1,41 @@ + * Object::add_extension('MyObject', "FulltextSearchable('MySearchableField,'MyOtherField')"); + * + * + * Caution: This is a wrapper method that should only be used in _config.php, + * and only be called once in your code. + * + * @param Array $searchableClasses The extension will be applied to all DataObject subclasses + * listed here. Default: {@link SiteTree} and {@link File}. */ static function enable($searchableClasses = array('SiteTree', 'File')) { $defaultColumns = array( @@ -26,14 +51,19 @@ class FulltextSearchable extends DataObjectDecorator { throw new Exception("FulltextSearchable::enable() I don't know the default search columns for class '$class'"); } } - self::$searchableClasses = $searchableClasses; + self::$searchable_classes = $searchableClasses; Object::add_extension("ContentController", "ContentControllerSearchExtension"); } + /** + * @param Array|String $searchFields Comma-separated list (or array) of database column names + * that can be searched on. Used for generation of the database index defintions. + */ function __construct($searchFields) { if(is_array($searchFields)) $this->searchFields = implode(',', $searchFields); else $this->searchFields = $searchFields; + parent::__construct(); } @@ -52,4 +82,14 @@ class FulltextSearchable extends DataObjectDecorator { ); } } + + /** + * Shows all classes that had the {@link FulltextSearchable} extension applied through {@link enable()}. + * + * @return Array + */ + function get_searchable_classes() { + return self::$searchable_classes; + } + } \ No newline at end of file diff --git a/tests/search/FulltextSearchableTest.php b/tests/search/FulltextSearchableTest.php new file mode 100644 index 000000000..38680a669 --- /dev/null +++ b/tests/search/FulltextSearchableTest.php @@ -0,0 +1,44 @@ +orig['File_searchable'] = Object::has_extension('File', 'FulltextSearchable'); + $this->orig['SiteTree_searchable'] = Object::has_extension('SiteTree', 'FulltextSearchable'); + + // TODO This shouldn't need all arguments included + Object::remove_extension('File', 'FulltextSearchable(\'Filename,Title,Content\')'); + Object::remove_extension('SiteTree', 'FulltextSearchable(\'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords\')'); + } + + function tearDown() { + // TODO This shouldn't need all arguments included + if($this->orig['File_searchable']) Object::add_extension('File', 'FulltextSearchable(\'Filename,Title,Content\')'); + if($this->orig['SiteTree_searchable']) Object::add_extension('SiteTree', 'FulltextSearchable(\'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords\')'); + + parent::tearDown(); + } + + function testEnable() { + FulltextSearchable::enable(); + $this->assertTrue(Object::has_extension('SiteTree', 'FulltextSearchable')); + $this->assertTrue(Object::has_extension('File', 'FulltextSearchable')); + } + + function testEnableWithCustomClasses() { + FulltextSearchable::enable(array('SiteTree')); + $this->assertTrue(Object::has_extension('SiteTree', 'FulltextSearchable')); + + // TODO This shouldn't need all arguments included + Object::remove_extension('File', 'FulltextSearchable(\'Filename,Title,Content\')'); + + $this->assertFalse(Object::has_extension('File', 'FulltextSearchable')); + } + +} \ No newline at end of file