      1: <?php
      2:
      3: /**
      4:  *
      5:  * Test case for the SearchProviderTags class
      6:  *
      7:  * @package alpha::tests
      8:  * @since 1.2.3
      9:  * @author John Collins <dev@alphaframework.org>
     10:  * @version $Id: SearchProviderTags_Test.php 1701 2013-12-18 22:33:25Z alphadevx $
     11:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     12:  * @copyright Copyright (c) 2013, John Collins (founder of Alpha Framework).
     13:  * All rights reserved.
     14:  *
     15:  * <pre>
     16:  * Redistribution and use in source and binary forms, with or
     17:  * without modification, are permitted provided that the
     18:  * following conditions are met:
     19:  *
     20:  * * Redistributions of source code must retain the above
     21:  *   copyright notice, this list of conditions and the
     22:  *   following disclaimer.
     23:  * * Redistributions in binary form must reproduce the above
     24:  *   copyright notice, this list of conditions and the
     25:  *   following disclaimer in the documentation and/or other
     26:  *   materials provided with the distribution.
     27:  * * Neither the name of the Alpha Framework nor the names
     28:  *   of its contributors may be used to endorse or promote
     29:  *   products derived from this software without specific
     30:  *   prior written permission.
     31:  *
     32:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     33:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     34:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     35:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     36:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     37:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     38:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     39:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     40:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     41:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     42:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     43:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     44:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     45:  * </pre>
     46:  *
     47:  */
     48: class SearchProviderTags_Test extends PHPUnit_Framework_TestCase {
     49:     /**
     50:      * An ArticleObject for testing
     51:      *
     52:      * @var ArticleObject
     53:      * @since 1.2.3
     54:      */
     55:     private $article;
     56:
     57:     /**
     58:      * Called before the test functions will be executed
     59:      * this function is defined in PHPUnit_TestCase and overwritten
     60:      * here
     61:      *
     62:      * @since 1.2.3
     63:      */
     64:     protected function setUp() {
     65:         $tag = new TagObject();
     66:         $tag->rebuildTable();
     67:
     68:         $denum = new DEnum();
     69:         $denum->rebuildTable();
     70:
     71:         $item = new DEnumItem();
     72:         $item->rebuildTable();
     73:
     74:         $article = new ArticleObject();
     75:         $article->rebuildTable();
     76:
     77:         $denum = new DEnum('ArticleObject::section');
     78:         $item->set('DEnumID', $denum->getOID());
     79:         $item->set('value', 'Test');
     80:         $item->save();
     81:
     82:         $this->article = $this->createArticleObject('unitTestArticle');
     83:     }
     84:
     85:     /**
     86:      * Called after the test functions are executed
     87:      * this function is defined in PHPUnit_TestCase and overwritten
     88:      * here
     89:      *
     90:      * @since 1.2.3
     91:      */
     92:     protected function tearDown() {
     93:         $article = new ArticleObject();
     94:         $article->dropTable();
     95:
     96:         $tag = new TagObject();
     97:         $tag->dropTable();
     98:
     99:         $denum = new DEnum();
    100:         $denum->dropTable();
    101:
    102:         $item = new DEnumItem();
    103:         $item->dropTable();
    104:
    105:         unset($this->article);
    106:     }
    107:
    108:     /**
    109:      * Creates an article object for testing
    110:      *
    111:      * @return ArticleObject
    112:      * @since 1.2.3
    113:      */
    114:     private function createArticleObject($name) {
    115:         $article = new ArticleObject();
    116:         $article->set('title', $name);
    117:         $article->set('description', 'A test article called unitTestArticle with some stop words and the unitTestArticle title twice');
    118:         $article->set('author', 'blah');
    119:         $article->set('content', 'blah');
    120:
    121:         return $article;
    122:     }
    123:
    124:     /**
    125:      * Testing that the index method is generating tags as expected
    126:      *
    127:      * @since 1.2.3
    128:      */
    129:     public function testIndex() {
    130:         $this->article->save();
    131:
    132:         $tag = new TagObject();
    133:         $tag->dropTable();
    134:         $tag->rebuildTable();
    135:
    136:         $provider = SearchProviderFactory::getInstance('SearchProviderTags');
    137:         $provider->index($this->article);
    138:
    139:         $tags = $this->article->getPropObject('tags')->getRelatedObjects();
    140:
    141:         $found = false;
    142:         foreach($tags as $tag) {
    143:             if($tag->get('content') == 'unittestarticle') {
    144:                 $found = true;
    145:                 break;
    146:             }
    147:         }
    148:         $this->assertTrue($found, 'Testing that the index method is generating tags as expected');
    149:     }
    150:
    151:     /**
    152:      * Testing that tags have been deleted once a DAO has been deleted from the search index
    153:      *
    154:      * @since 1.2.3
    155:      */
    156:     public function testDelete() {
    157:         $this->article->save();
    158:         $tags = $this->article->getPropObject('tags')->getRelatedObjects();
    159:
    160:         $this->assertTrue(count($tags) > 0, 'Confirming that tags exist after saving the article (ArticleObject::after_save_callback())');
    161:
    162:         $provider = SearchProviderFactory::getInstance('SearchProviderTags');
    163:         $provider->delete($this->article);
    164:
    165:         $tags = $this->article->getPropObject('tags')->getRelatedObjects();
    166:
    167:         $this->assertTrue(count($tags) == 0, 'Testing that tags have been deleted once a DAO has been deleted from the search index');
    168:     }
    169:
    170:     /**
    171:      * Testing the search method for expected results
    172:      *
    173:      * @since 1.2.3
    174:      */
    175:     public function testSearch() {
    176:         $this->article->save();
    177:
    178:         $provider = SearchProviderFactory::getInstance('SearchProviderTags');
    179:         $results = $provider->search('unitTestArticle');
    180:
    181:         $this->assertTrue(count($results) == 1, 'Testing the search method for expected results');
    182:         $this->assertEquals($this->article->getOID(), $results[0]->getOID(), 'Testing the search method for expected results');
    183:
    184:         $results = $provider->search('unitTestArticle', 'PersonObject');
    185:
    186:         $this->assertTrue(count($results) == 0, 'Testing the search method honours returnType filtering');
    187:     }
    188:
    189:     /**
    190:      * Testing the method for getting the expected number of results
    191:      *
    192:      * @since 1.2.3
    193:      */
    194:     public function testGetNumberFound() {
    195:         $this->article->save();
    196:
    197:         $provider = SearchProviderFactory::getInstance('SearchProviderTags');
    198:         $results = $provider->search('unitTestArticle');
    199:
    200:         $this->assertTrue($provider->getNumberFound() == 1, 'Testing the method for getting the expected number of results');
    201:
    202:         $article2 = $this->createArticleObject('unitTestArticle 2');
    203:         $article2->save();
    204:
    205:         $article3 = $this->createArticleObject('unitTestArticle 3');
    206:         $article3->save();
    207:
    208:         $results = $provider->search('unitTestArticle');
    209:
    210:         $this->assertTrue($provider->getNumberFound() == 3, 'Testing the method for getting the expected number of results');
    211:     }
    212:
    213:     /**
    214:      * Testing the method for getting related objects
    215:      *
    216:      * @since 1.2.3
    217:      */
    218:     public function testGetRelated() {
    219:         $this->article->save();
    220:
    221:
    222:         $article2 = $this->createArticleObject('unitTestArticle 2');
    223:         $article2->save();
    224:
    225:         $article3 = $this->createArticleObject('unitTestArticle 3');
    226:         $article3->save();
    227:
    228:         $provider = SearchProviderFactory::getInstance('SearchProviderTags');
    229:         $results = $provider->getRelated($this->article);
    230:
    231:         $this->assertTrue(count($results) == 2, 'Testing the method for getting related objects');
    232:
    233:         $results = $provider->getRelated($this->article, 'all', 0, 1);
    234:
    235:         $this->assertTrue(count($results) == 1, 'Testing the method for getting related objects honours limit param');
    236:
    237:         $results = $provider->getRelated($this->article, 'PersonObject');
    238:
    239:         $this->assertTrue(count($results) == 0, 'Testing the get related objects method honours returnType filtering');
    240:     }
    241: }
    242:
    243: ?>