Skip to content

Commit

Permalink
weight now hardcoded into the tagadelic_taxonomy module tagbuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Bèr Kessels committed Dec 31, 2012
1 parent 76143af commit 864e13d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 10 deletions.
38 changes: 29 additions & 9 deletions tagadelic_taxonomy.module
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,40 @@ function tagadelic_taxonomy_theme($existing, $type, $theme, $path) {
}

function tagadelic_taxonomy_get_tags() {
$terms = array();
$tags = array();

/**
see http://drupal.stackexchange.com/q/54483/787
$query = db_select('taxonomy_term_data', 't');
$result = $query
->fields('t')
->range(0, 60)
->execute();

foreach ($result as $term) {
$tag = new TagadelicTag($term->tid, $term->name, 1);
$query->addField('t', 'tid');
$query->addField('t', 'name');
$query->addField('t', 'description');
$query->addField('COUNT(i.nid)', 'count');

$query->leftjoin('taxonomy_index', 'i', 'i.nid = t.nid');
$query->range(0, 60)
->groupBy('i.nid');

print($query);

result = $query->execute();*/

$result = db_query("SELECT t.tid, t.name, t.description, COUNT(i.nid) AS count
FROM taxonomy_index AS i
LEFT JOIN taxonomy_term_data AS t ON i.tid = t.tid
GROUP BY i.tid
LIMIT 60");

/*foreach ($result as $term) {*/
while($term = $result->fetchObject()) {
$tag = new TagadelicTag($term->tid, $term->name, $term->count);
$tag->set_link("taxonomy/term/{$term->tid}");
$tag->set_weight(1);

$terms[] = $tag;
$tags[] = $tag;
}

return $terms;
return $tags;
}

57 changes: 56 additions & 1 deletion tests/tagadelic_taxonomy.test
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,63 @@ class TagadelicTaxonomyTestCase extends DrupalWebTestCase {
$this->assertEqual(60, $amount);
}

public function testHasWeightedTags() {
$this->createVocAndTags(6);
$this->createNodesWithTags(10);
$this->drupalGet("tagadelic_taxonomy");
$weight = 1;
foreach($this->tags as $tag) {
/*$this->assertTagHasWeight($tag->name, $weight++);*/
$this->assertTagHasWeight($tag->name, 1);
}
}

private function createVocAndTags($amount_of_tags) {
$tx_test = new TaxonomyWebTestCase();

$this->tags = array();
$this->vocabulary = $tx_test->createVocabulary();
for ($i = 0; $i < $amount_of_tags; $i++) {
$this->tags[] = $tx_test->createTerm($this->vocabulary);
}
return $this;
}

private function createNodesWithTags() {
/**
* Creates $amount nodes with terms attached.
*
*
* Fuck it, I am poking around in the database directly instead of testing
* and preparing all this field, entity, admin-user and whatnot. I am not
* interested in whether or not we can store nodes with tags, only that they
* are there. By adding them to the database, we achieve that.
*/
private function createNodesWithTags($amount) {
$this->nodes = array();
$attachable = $this->tags;
for ($i = 0; $i < $amount; $i++) {
// Post an article.
$node = new StdClass();
$node->title = $this->randomName();
$node->type = "story";
node_save($node);
$this->nodes[] = $node;

// Attach the terms
$query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created'));
foreach($attachable as $tag) {
$query->values(array(
'nid' => $node->nid,
'tid' => $tag->tid,
'sticky' => TRUE,
'created' => $node->created,
));
}
$query->execute();

//remove one tag, so the next node gets one less tag attached.
array_shift($attachable);
}
}

/**
Expand All @@ -91,4 +138,12 @@ class TagadelicTaxonomyTestCase extends DrupalWebTestCase {
$xpath = "//ul[@class='tag-cloud']/li/a";
return $this->assertHasXpath($xpath, $message, $group);
}

private function assertTagHasWeight($name, $weight, $message = '', $group = 'Other') {
if (empty($message)) {
$message = "Tag with name '{$name}' and class 'weight-{$weight}' found";
}
$xpath = "//*/ul[@class='tag-cloud']/li/a[contains(text(),'{$name}')][@class='weight-{$weight}']";
return $this->assertHasXpath($xpath, $message, $group = 'Other');
}
}

0 comments on commit 864e13d

Please sign in to comment.