[pLog-svn] r4027 - in plog/branches/lifetype-1.1.1/class: action
dao summary/action test/tests/dao
oscar at devel.lifetype.net
oscar at devel.lifetype.net
Sat Sep 23 10:58:50 GMT 2006
Author: oscar
Date: 2006-09-23 10:58:50 +0000 (Sat, 23 Sep 2006)
New Revision: 4027
Added:
plog/branches/lifetype-1.1.1/class/test/tests/dao/searchengine_test.class.php
Modified:
plog/branches/lifetype-1.1.1/class/action/searchaction.class.php
plog/branches/lifetype-1.1.1/class/dao/searchengine.class.php
plog/branches/lifetype-1.1.1/class/summary/action/summarysearchaction.class.php
Log:
articles with a future date should not appear anymore in searches
Modified: plog/branches/lifetype-1.1.1/class/action/searchaction.class.php
===================================================================
--- plog/branches/lifetype-1.1.1/class/action/searchaction.class.php 2006-09-23 10:47:03 UTC (rev 4026)
+++ plog/branches/lifetype-1.1.1/class/action/searchaction.class.php 2006-09-23 10:58:50 UTC (rev 4027)
@@ -57,13 +57,15 @@
$searchResults = $searchEngine->search( $this->_blogInfo->getId(),
$this->_searchTerms,
POST_STATUS_PUBLISHED,
+ false,
$this->_page, // page
$itemsPerPage // items per page
);
// and the total number of items
$numSearchResults = $searchEngine->getNumSearchResults( $this->_blogInfo->getId(),
$this->_searchTerms,
- POST_STATUS_PUBLISHED );
+ POST_STATUS_PUBLISHED,
+ false );
// MARKWU: I add the searchterms variable for smarty/plog template
$searchTerms = $searchEngine->getAdaptSearchTerms( $this->_searchTerms );
Modified: plog/branches/lifetype-1.1.1/class/dao/searchengine.class.php
===================================================================
--- plog/branches/lifetype-1.1.1/class/dao/searchengine.class.php 2006-09-23 10:47:03 UTC (rev 4026)
+++ plog/branches/lifetype-1.1.1/class/dao/searchengine.class.php 2006-09-23 10:58:50 UTC (rev 4027)
@@ -70,6 +70,7 @@
* @param blogId The blog id where we're searching, or -1 if we're doing a global search
* @param searchTerms A string containing the search terms
* @param status In case we're looking for a particular status
+ * @param includeFuture
* @param page Page of results to return
* @param itemsPerPage Number of items per page
* @return
@@ -77,7 +78,7 @@
* @see searchCustomFields
* @see searchComments
*/
- function search( $blogId, $searchTerms, $status = POST_STATUS_PUBLISHED, $page = -1, $itemsPerPage = -1 )
+ function search( $blogId, $searchTerms, $status = POST_STATUS_PUBLISHED, $includeFuture = true, $page = -1, $itemsPerPage = -1 )
{
$prefix = $this->getPrefix();
$query = "SELECT DISTINCT a.id AS id FROM {$prefix}articles a ".
@@ -89,8 +90,11 @@
if( $status != -1 )
$query .= " AND a.status = ".$status;
+ if( !$includeFuture )
+ $query .= " AND a.date < NOW()";
+
$query .= " ORDER BY a.date DESC";
-
+
$result = $this->Execute( $query, $page, $itemsPerPage );
include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" );
@@ -117,7 +121,7 @@
* @param status
* @return
*/
- function getNumSearchResults( $blogId, $searchTerms, $status )
+ function getNumSearchResults( $blogId, $searchTerms, $status, $includeFuture = true )
{
$prefix = $this->getPrefix();
$query = $this->getArticleSearchConditions( $searchTerms );
@@ -128,6 +132,9 @@
if( $status != -1 )
$query .= " AND a.status = ".$status;
+ if( !$includeFuture )
+ $query .= " AND a.date < NOW()";
+
$total = $this->getNumItems( "{$prefix}articles a", $query, "a.id" );
return( $total );
}
@@ -196,6 +203,7 @@
* @param searchType One of the following: SEARCH_ARTICLE, SEARCH_BLOG or SEARCH_GALLERYRESOURCE
* @param status The status. Each item has its own constants for status codes, so this value will depend
* on what we're looking for.
+ * @param includeFuture Whether to include posts with a future date in the results
* @param page The page of results to get
* @param itemsPerPage Number of items per page
* @return
@@ -204,14 +212,14 @@
* @see searchCustomFields
* @see searchComments
*/
- function siteSearch( $searchTerms, $searchType, $status = -1, $page = -1, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
+ function siteSearch( $searchTerms, $searchType, $status = -1, $includeFuture = true, $page = -1, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
{
switch ( $searchType )
{
case SEARCH_BLOG:
if ( $status == -1 )
$status = BLOG_STATUS_ACTIVE;
- $result = $this->searchBlogs( $searchTerms, $status, $page, $itemsPerPage );
+ $result = $this->searchBlogs( $searchTerms, $status, $includeFuture, $page, $itemsPerPage );
break;
case SEARCH_GALLERYRESOURCE:
$result = $this->searchGalleryResources( '_all_', $searchTerms, $page, $itemsPerPage );
@@ -220,7 +228,7 @@
// search articles and any other value of the $searchType parameter
if ( $status == -1 )
$status = POST_STATUS_PUBLISHED;
- $result = $this->search( -1, $searchTerms, $status, $page, $itemsPerPage );
+ $result = $this->search( -1, $searchTerms, $status, $includeFuture, $page, $itemsPerPage );
}
return $result;
}
@@ -232,15 +240,16 @@
* @param searchType One of the following: SEARCH_ARTICLE, SEARCH_BLOG or SEARCH_GALLERYRESOURCE
* @param status The status. Each item has its own constants for status codes, so this value will depend
* on what we're looking for.
+ * @param includeFuture whether to include future articles in the search
* @return The number of matching items for the given search terms
*/
- function getNumSiteSearchResults( $searchTerms, $searchType, $status = -1 )
+ function getNumSiteSearchResults( $searchTerms, $searchType, $status = -1, $includeFuture = true )
{
if( $searchType == SEARCH_ARTICLE ) {
if( $status == -1 )
$status = POST_STATUS_PUBLISHED;
- $result = $this->getNumSearchResults( -1, $searchTerms, $status );
+ $result = $this->getNumSearchResults( -1, $searchTerms, $status, $includeFuture );
}
elseif( $searchType == SEARCH_BLOG ) {
if ( $status == -1 )
Modified: plog/branches/lifetype-1.1.1/class/summary/action/summarysearchaction.class.php
===================================================================
--- plog/branches/lifetype-1.1.1/class/summary/action/summarysearchaction.class.php 2006-09-23 10:47:03 UTC (rev 4026)
+++ plog/branches/lifetype-1.1.1/class/summary/action/summarysearchaction.class.php 2006-09-23 10:58:50 UTC (rev 4027)
@@ -47,11 +47,12 @@
$results = $search->siteSearch( $this->_searchTerms,
$this->_searchType,
-1,
+ false,
View::getCurrentPageFromRequest(),
$itemsPerPage );
// get the total number of results, for the pager
- $numResults = $search->getNumSiteSearchResults( $this->_searchTerms, $this->_searchType );
+ $numResults = $search->getNumSiteSearchResults( $this->_searchTerms, $this->_searchType, -1, false );
// no results
if( !$results || empty($results)) {
Added: plog/branches/lifetype-1.1.1/class/test/tests/dao/searchengine_test.class.php
===================================================================
--- plog/branches/lifetype-1.1.1/class/test/tests/dao/searchengine_test.class.php 2006-09-23 10:47:03 UTC (rev 4026)
+++ plog/branches/lifetype-1.1.1/class/test/tests/dao/searchengine_test.class.php 2006-09-23 10:58:50 UTC (rev 4027)
@@ -0,0 +1,98 @@
+<?php
+
+ include_once( PLOG_CLASS_PATH."class/test/helpers/lifetypetestcase.class.php" );
+ include_once( PLOG_CLASS_PATH."class/test/helpers/testtools.class.php" );
+ include_once( PLOG_CLASS_PATH."class/dao/searchengine.class.php" );
+
+ /**
+ * \ingroup Test
+ *
+ * Test cases for the SearchEngine class
+ */
+ class SearchEngine_Test extends LifeTypeTestCase
+ {
+ var $user;
+ var $blog;
+ var $articles;
+ var $cat;
+ var $art1;
+ var $art2;
+
+ /**
+ * @private
+ * Set up the test data
+ */
+ function setUp()
+ {
+ // create the test data
+ $this->user = TestTools::createUser();
+ $this->blog = TestTools::createBlog( $this->user->getId());
+ $this->cat = TestTools::createArticleCategory( $this->blog->getId());
+
+ // create a post with current date
+ $this->art1 = TestTools::createArticle( $this->blog->getId(), $this->user->getId(), Array( $this->cat->getId()));
+ // and one with future date
+ $t = new Timestamp();
+ $t->addSeconds( 60 * 60 * 10 );
+ $this->art2 = TestTools::createArticle( $this->blog->getId(), $this->user->getId(), Array( $this->cat->getId()), POST_STATUS_PUBLISHED, $t );
+ }
+
+ /**
+ * @private
+ * Delete the test data
+ */
+ function tearDown()
+ {
+ TestTools::deleteDaoTestData(
+ Array( $this->user, $this->blog, $this->cat, $this->art1, $this->art2 )
+ );
+ }
+
+ /**
+ * make sure that future posts are not returned as search results
+ */
+ function testSearchIgnoreFuturePosts()
+ {
+ // check that when searching for this specific string, we only get one match (there should only be one test article)
+ $searchEngine = new SearchEngine();
+ $results = $searchEngine->search( $this->blog->getId(), "test article", POST_STATUS_PUBLISHED, false );
+ $this->assertEquals( 1, count( $results ), "There should only be one article in the search results" );
+
+ // check that the article with the future date is not part of the results
+ foreach( $results as $result ) {
+ if( $result->getType() == SEARCH_RESULT_ARTICLE ) {
+ $article = $result->getArticle();
+ $this->assertFalse( ($article->getId() == $this->art2->getId()), "The future article was found as part of the search results!" );
+ }
+ }
+
+ // and finally check that the amount of search results returned is valid
+ $this->assertEquals( 1, $searchEngine->getNumSearchResults( $this->blog->getId(), "test article", POST_STATUS_PUBLISHED, false ),
+ "The future article was counted by SearchEngine::getNumSearchResults()!" );
+ }
+
+ /**
+ * make sure that future posts are not returned as search results when
+ * doing site-wide searches
+ */
+ function testSiteSearchIgnoreFuturePosts()
+ {
+ // check that when searching for this specific string, we only get one match (there should only be one test article)
+ $searchEngine = new SearchEngine();
+ $results = $searchEngine->siteSearch( "test article", SEARCH_ARTICLE, POST_STATUS_PUBLISHED, false );
+ $this->assertEquals( 1, count( $results ), "There should only be one article in the search results" );
+
+ // check that the article with the future date is not part of the results
+ foreach( $results as $result ) {
+ if( $result->getType() == SEARCH_RESULT_ARTICLE ) {
+ $article = $result->getArticle();
+ $this->assertFalse( ($article->getId() == $this->art2->getId()), "The future article was found as part of the search results!" );
+ }
+ }
+
+ // and finally check that the amount of search results returned is valid
+ $this->assertEquals( 1, $searchEngine->getNumSiteSearchResults( "test article", SEARCH_ARTICLE, POST_STATUS_PUBLISHED, false ),
+ "The future article was counted by SearchEngine::getNumSiteSearchResults()!" );
+ }
+ }
+?>
\ No newline at end of file
More information about the pLog-svn
mailing list