From 83ad8d48a99fcc82c2c0c4a24a4ed6923c506a1e Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 15 Sep 2011 16:00:54 +0200 Subject: [PATCH] ENHANCEMENT Added File.ShowInSearch flag to mirror the existing SiteTree.ShowInSearch flag - e.g. useful to limit visibility of user-uploaded files. Enforced in MySQLDatabase->searchEngine(). --- core/model/MySQLDatabase.php | 5 +++++ filesystem/File.php | 10 +++++++--- tests/search/SearchFormTest.php | 22 +++++++++++++++++++++- tests/search/SearchFormTest.yml | 9 ++++++++- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/core/model/MySQLDatabase.php b/core/model/MySQLDatabase.php index 164ef6f26..0c44e3a2b 100755 --- a/core/model/MySQLDatabase.php +++ b/core/model/MySQLDatabase.php @@ -767,6 +767,11 @@ class MySQLDatabase extends SS_Database { // Always ensure that only pages with ShowInSearch = 1 can be searched $extraFilters['SiteTree'] .= " AND ShowInSearch <> 0"; + + // File.ShowInSearch was added later, keep the database driver backwards compatible + // by checking for its existence first + $fields = $this->fieldList('File'); + if(array_key_exists('ShowInSearch', $fields)) $extraFilters['File'] .= " AND ShowInSearch <> 0"; $limit = $start . ", " . (int) $pageLength; diff --git a/filesystem/File.php b/filesystem/File.php index 382e73e30..e71e77a44 100755 --- a/filesystem/File.php +++ b/filesystem/File.php @@ -74,7 +74,9 @@ class File extends DataObject { "Title" => "Varchar(255)", "Filename" => "Text", "Content" => "Text", - "Sort" => "Int" + "Sort" => "Int", + // Only applies to files, doesn't inherit for folder + 'ShowInSearch' => 'Boolean(1)', ); static $has_one = array( @@ -90,8 +92,10 @@ class File extends DataObject { "BackLinkTracking" => "SiteTree", ); - static $defaults = array(); - + static $defaults = array( + "ShowInSearch" => 1, + ); + static $extensions = array( "Hierarchy", ); diff --git a/tests/search/SearchFormTest.php b/tests/search/SearchFormTest.php index a3b392d0d..a55d299c6 100644 --- a/tests/search/SearchFormTest.php +++ b/tests/search/SearchFormTest.php @@ -170,7 +170,7 @@ class SearchFormTest extends FunctionalTest { $member->logOut(); } - function testDisabledShowInSearchFlagNotIncluded() { + function testDisabledShowInSearchFlagNotIncludedForSiteTree() { $sf = new SearchForm($this->mockController, 'SearchForm'); $page = $this->objFromFixture('SiteTree', 'dontShowInSearchPage'); @@ -181,6 +181,26 @@ class SearchFormTest extends FunctionalTest { 'Page with "Show in Search" disabled doesnt show' ); } + + function testDisabledShowInSearchFlagNotIncludedForFiles() { + $sf = new SearchForm($this->mockController, 'SearchForm'); + + $dontShowInSearchFile = $this->objFromFixture('File', 'dontShowInSearchFile'); + $showInSearchFile = $this->objFromFixture('File', 'showInSearchFile'); + $results = $sf->getResults(null, array('Search'=>'dontShowInSearchFile')); + $this->assertNotContains( + $dontShowInSearchFile->ID, + $results->column('ID'), + 'File with "Show in Search" disabled doesnt show' + ); + + $results = $sf->getResults(null, array('Search'=>'showInSearchFile')); + $this->assertContains( + $showInSearchFile->ID, + $results->column('ID'), + 'File with "Show in Search" enabled can be found' + ); + } function testSearchTitleAndContentWithSpecialCharacters() { $sf = new SearchForm($this->mockController, 'SearchForm'); diff --git a/tests/search/SearchFormTest.yml b/tests/search/SearchFormTest.yml index 91c18cfb0..a88861ae8 100644 --- a/tests/search/SearchFormTest.yml +++ b/tests/search/SearchFormTest.yml @@ -33,4 +33,11 @@ SiteTree: ShowInSearch: 0 pageWithSpecialChars: Title: Brötchen - Content: Frisch vom Bäcker \ No newline at end of file + Content: Frisch vom Bäcker +File: + showInSearchFile: + Title: showInSearchFile + ShowInSearch: 1 + dontShowInSearchFile: + Title: dontShowInSearchFile + ShowInSearch: 0 \ No newline at end of file