/[drupal]/contributions/modules/biblio/tests/keyword.test
ViewVC logotype

Diff of /contributions/modules/biblio/tests/keyword.test

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1 by rjerome, Mon Nov 16 02:24:22 2009 UTC revision 1.1.2.1 by rjerome, Mon Nov 16 02:24:22 2009 UTC
# Line 0  Line 1 
1    <?php
2    // $Id$
3    
4    /**
5     * @file
6     * Tests for keyword functions.
7     */
8    
9    /**
10    * Class with common helper methods.
11    */
12    class BiblioKeywordWebTestCase extends BiblioWebTestCase {
13    
14      function setUp() {
15        require_once(drupal_get_path('module', 'biblio') .'/biblio.keywords.inc');
16      }
17    
18      /**
19       * Returns a new keyword with random properties.
20       */
21      function createKeyword() {
22        $keyword = array();
23        $keyword['word'] = $this->randomName();
24        biblio_save_keyword($keyword);
25        $this->kids[] = $keyword['kid'];
26        return $keyword;
27      }
28    
29    }
30    
31    /**
32     * Unit tests for keyword functions.
33     */
34    class BiblioKeywordUnitTest extends BiblioKeywordWebTestCase {
35    
36      public static function getInfo() {
37        return array(
38          'name' => 'Biblio keyword unit tests',
39          'description' => 'Unit tests for keyword functions.',
40          'group' => 'Biblio',
41        );
42      }
43      function testBiblioSaveKeyword() {
44        $keyword = $this->createKeyword();
45        $this->assertTrue($keyword['kid'], t('Created and saved a single keyword'));
46      }
47      function testBiblioDeleteKeyword() {
48        $keyword = $this->createKeyword();
49        $num_deleted = biblio_delete_keyword($keyword['kid']);
50        $this->assertEqual($num_deleted, 1, t('Deleted a single keyword'));
51      }
52      function testBiblioGetKeywordById() {
53        $keyword = $this->createKeyword();
54        $word = biblio_get_keyword_by_name($keyword['kid']);
55        $this->assertIdentical($keyword, $word, t('Loaded keyword by ID'));
56      }
57      function testBiblioGetKeywordByName() {
58        $keyword = $this->createKeyword();
59    
60        // Load the term with the exact name.
61        $word = biblio_get_keyword_by_name($keyword['word']);
62        $this->assertEqual($word->word, $keyword['word'], t('Keyword loaded using exact name.'));
63    
64        // Load the term with space concatenated.
65        $word  = biblio_get_keyword_by_name('  ' . $keyword['word'] . '   ');
66        $this->assertEqual($word->word, trim('  ' . $keyword['word'] . '   '), t('Keyword loaded with extra whitespace.'));
67    
68        // Load the term with name uppercased.
69        $word = biblio_get_keyword_by_name(strtoupper($keyword['word']));
70        $this->assertEqual($word->word, $keyword['word'], t('Keyword loaded with uppercased name.'));
71    
72        // Load the term with name lowercased.
73        $word = biblio_get_keyword_by_name(strtolower($keyword['word']));
74        $this->assertEqual($word->word, $keyword['word'], t('Keyword loaded with lowercased name.'));
75    
76        // Try to load an invalid term name.
77        $word = biblio_get_keyword_by_name('Banana');
78        $this->assertFalse($word, t('Tried to load an invalid keyword'));
79    
80        // Try to load the term using a substring of the name.
81        $word = biblio_get_keyword_by_name(drupal_substr($keyword['word'], 2));
82        $this->assertFalse($word, t('Tried to load a keyword using a substring of the word'));
83      }
84    
85    
86      function testBiblioUpdateKeywords() {
87        $term1 = $this->createKeyword();
88        $node = $this->createNode();
89        $node->biblio_keywords = array($term1['word']);
90        $old_vid = $node->vid;
91    
92        $node->biblio_keywords[0] .= 'xxx';
93        $node->revision = TRUE;
94        node_save($node);
95    
96        $node = node_load($node->nid, NULL, TRUE);
97        foreach($node->biblio_contributors[1] as $author) {
98          $this->cids[] = $author['cid'];  // cache the cid's for later removal
99        }
100        foreach($node->biblio_keywords as $kid => $value) {
101          $this->assertEqual($value, $term1['word'].'xxx', 'Loaded updated keyword');
102          $this->kids[] = $kid; // add the new kids to the global array so we can delete them on tear down.
103        }
104        $node = node_load($node->nid, $old_vid, TRUE);
105        foreach($node->biblio_keywords as $kid => $value) {
106          $this->assertEqual($value, $term1['word'], 'Loaded previous revision prior to update');
107          $this->kids[] = $kid; // add the new kids to the global array so we can delete them on tear down.
108        }
109        $num_deleted = 0;
110        foreach (array_unique($this->kids) as $kid) {
111          $num_deleted += biblio_delete_keyword($kid);
112        }
113        $this->assertEqual($num_deleted, count(array_unique($this->kids)), "Deleted $num_deleted of ".count(array_unique($this->kids))." keywords");
114    
115        $node = node_load($node->nid, NULL, TRUE);
116        $this->assertFalse(count($node->biblio_keywords), "Loaded node which no longer has any keywords");
117      }
118    
119      function testBiblioDeleteOrphanKeywords() {
120        $this->createKeyword();
121        $this->createKeyword();
122        $count = count($this->kids);
123        $num_records_before = db_query('SELECT COUNT(*) FROM {biblio_keyword_data} WHERE kid NOT IN (SELECT DISTINCT(kid) FROM {biblio_keyword})')->fetchField();
124        biblio_delete_orphan_keywords(TRUE);
125        $num_records_after = db_query('SELECT COUNT(*) FROM {biblio_keyword_data} WHERE kid NOT IN (SELECT DISTINCT(kid) FROM {biblio_keyword})')->fetchField();
126        $this->assertEqual($num_records_before, $num_records_after+$count, "Deleted $num_records_before orphan keywords");
127      }
128    
129      function testBiblioExplodeKeywords() {
130        $keywords = array();
131        $exploded = array();
132        $words = array();
133        $sep = variable_get('biblio_keyword_sep', ',');
134        $wrong_sep = ($sep == ',') ? ';' : ',';
135        for($i=0; $i < 4; $i++){
136          $words[] = $this->randomName();
137        }
138        $string = implode($sep, $words);
139        $exploded = biblio_explode_keywords($string);
140        $this->assertIdentical($words, $exploded, 'Exploded keyword string with correct separator');
141        $string = implode($wrong_sep, $words);
142        $exploded = biblio_explode_keywords($string);
143        $this->assertNotIdentical($words, $exploded, 'Tried to explode keyword string with incorrect separator');
144        $words[] = '"'.'word1, word2'.'"';
145        $string = implode($sep, $words);
146        $exploded = biblio_explode_keywords($string);
147        $this->assertIdentical($words, $exploded);
148      }
149    }
150    
151    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.3