| 1 |
<?php
|
| 2 |
|
| 3 |
class MoreLikeThisBase extends DrupalWebTestCase {
|
| 4 |
|
| 5 |
function setUp() {
|
| 6 |
parent::setUp('morelikethis');
|
| 7 |
|
| 8 |
// Create and login user
|
| 9 |
$admin_user = $this->drupalCreateUser();
|
| 10 |
$this->drupalLogin($admin_user);
|
| 11 |
}
|
| 12 |
|
| 13 |
function tearDown() {
|
| 14 |
parent::tearDown();
|
| 15 |
}
|
| 16 |
|
| 17 |
function createVocabulary() {
|
| 18 |
$vocab = array();
|
| 19 |
$vocab['name'] = $this->randomName(20);
|
| 20 |
taxonomy_save_vocabulary($vocab);
|
| 21 |
return $vocab;
|
| 22 |
}
|
| 23 |
|
| 24 |
function createTerm($vid) {
|
| 25 |
$term = array();
|
| 26 |
$term['vid'] = $vid;
|
| 27 |
$term['name'] = $this->randomName(30);
|
| 28 |
$term['description'] = $this->randomName(200);
|
| 29 |
taxonomy_save_term($term);
|
| 30 |
return $term;
|
| 31 |
}
|
| 32 |
}
|
| 33 |
|
| 34 |
class MoreLikeThisLookupTestCase extends MoreLikeThisBase {
|
| 35 |
|
| 36 |
function getInfo() {
|
| 37 |
return array(
|
| 38 |
'name' => t('Internal Lookup'),
|
| 39 |
'description' => t('Test the lookup mechanism for identifying internal likeness'),
|
| 40 |
'group' => t('More Like This')
|
| 41 |
);
|
| 42 |
}
|
| 43 |
|
| 44 |
function testLookup() {
|
| 45 |
$vocab = $this->createVocabulary();
|
| 46 |
$vid = $vocab['vid'];
|
| 47 |
$t1 = $this->createTerm($vid);
|
| 48 |
$t2 = $this->createTerm($vid);
|
| 49 |
$t3 = $this->createTerm($vid);
|
| 50 |
$t4 = $this->createTerm($vid);
|
| 51 |
$t5 = $this->createTerm($vid);
|
| 52 |
$t6 = $this->createTerm($vid);
|
| 53 |
|
| 54 |
$n1 = $this->createNode('page', $vid, $t1, $t2, $t3, $t4, $t5);
|
| 55 |
$n2 = $this->createNode('story', $vid, $t1, $t2, $t3, $t4);
|
| 56 |
$n3 = $this->createNode('page', $vid, $t1, $t2, $t3);
|
| 57 |
$n4 = $this->createNode('story', $vid, $t1, $t2);
|
| 58 |
$n5 = $this->createNode('page', $vid, $t1);
|
| 59 |
$n6 = $this->createNode('page', $vid, $t6);
|
| 60 |
|
| 61 |
variable_set('morelikethis_target_types_page', array('page' => 'page', 'story' => 'story'));
|
| 62 |
variable_set('morelikethis_count_page', '3');
|
| 63 |
variable_set('morelikethis_threshold_page', '0.5');
|
| 64 |
|
| 65 |
// Test filter on count & threshold
|
| 66 |
$results = morelikethis_find($n1->nid);
|
| 67 |
$this->assertEqual(count($results), 2, "Matched results equal expected for test1");
|
| 68 |
$this->assertLikeness($results[0], $n2->nid, 4);
|
| 69 |
$this->assertLikeness($results[1], $n3->nid, 3);
|
| 70 |
|
| 71 |
// Test filter on count (threshold misses)
|
| 72 |
$results = morelikethis_find($n3->nid);
|
| 73 |
$this->assertEqual(count($results), 3, "Matched results equal expected for test2");
|
| 74 |
$this->assertLikeness($results[0], $n2->nid, 3);
|
| 75 |
$this->assertLikeness($results[1], $n1->nid, 3);
|
| 76 |
$this->assertLikeness($results[2], $n4->nid, 2);
|
| 77 |
|
| 78 |
// Test filter on count (threshold misses)
|
| 79 |
$results = morelikethis_find($n5->nid);
|
| 80 |
$this->assertEqual(count($results), 3, "Matched results equal expected for test3");
|
| 81 |
$this->assertLikeness($results[0], $n4->nid, 1);
|
| 82 |
$this->assertLikeness($results[1], $n3->nid, 1);
|
| 83 |
$this->assertLikeness($results[2], $n2->nid, 1);
|
| 84 |
//$this->assertLikeness($results[3], $n1->nid, 1);
|
| 85 |
|
| 86 |
variable_set('morelikethis_target_types_page', array('page' => 'page'));
|
| 87 |
variable_del('morelikethis_count_page');
|
| 88 |
variable_del('morelikethis_threshold_page');
|
| 89 |
|
| 90 |
// Test on node type filtering
|
| 91 |
$results = morelikethis_find($n1->nid);
|
| 92 |
$this->assertEqual(count($results), 2, "Matched results equal expected for test4");
|
| 93 |
$this->assertLikeness($results[0], $n3->nid, 3);
|
| 94 |
$this->assertLikeness($results[1], $n5->nid, 1);
|
| 95 |
|
| 96 |
// Test on threshold boundary
|
| 97 |
variable_set('morelikethis_threshold_page', 0.6);
|
| 98 |
$results = morelikethis_find($n1->nid);
|
| 99 |
$this->assertEqual(count($results), 1, "Matched results equal expected for test5");
|
| 100 |
$this->assertLikeness($results[0], $n3->nid, 3);
|
| 101 |
|
| 102 |
$results = morelikethis_find($n6->nid);
|
| 103 |
$this->assertEqual(count($results), 0, "No results should be found");
|
| 104 |
}
|
| 105 |
|
| 106 |
function createNode() {
|
| 107 |
$args = func_get_args();
|
| 108 |
$type = array_shift($args);
|
| 109 |
$vid = array_shift($args);
|
| 110 |
$terms = array();
|
| 111 |
foreach ($args as $arg) {
|
| 112 |
$terms[] = $arg['name'];
|
| 113 |
}
|
| 114 |
|
| 115 |
$terms = drupal_implode_tags($terms);
|
| 116 |
$params = array(
|
| 117 |
'type' => $type,
|
| 118 |
'taxonomy' => array(
|
| 119 |
'tags' => array($vid => $terms),
|
| 120 |
),
|
| 121 |
'morelikethis' => array(
|
| 122 |
'terms' => $terms,
|
| 123 |
),
|
| 124 |
);
|
| 125 |
return $this->drupalCreateNode($params);
|
| 126 |
}
|
| 127 |
|
| 128 |
function assertLikeness($result, $expected_nid, $expected_hits) {
|
| 129 |
$this->assertEqual($result->nid, $expected_nid, "nid: {$result->nid} equals expected: {$expected_nid}");
|
| 130 |
$this->assertEqual($result->hits, $expected_hits, "hits: {$result->hits} equals expected: {$expected_hits}");
|
| 131 |
}
|
| 132 |
}
|