og_sm-8.x-1.0/og_sm_taxonomy/tests/OgSmTaxonomyApiTestCase.test
og_sm_taxonomy/tests/OgSmTaxonomyApiTestCase.test
<?php
/**
* @file
* Tests about the Site Taxonomies.
*/
/**
* Tests about the Site Taxonomy API.
*/
class OgSmTaxonomyApiTestCase extends OgSmWebTestCase {
/**
* Site Nodes to run the tests with.
*
* @var object
*/
private $siteNode1;
private $siteNode2;
/**
* Vocabulary "categories" to run tests with.
*
* @var object
*/
private $vocabCategories;
/**
* Vocabulary "tags" to run tests with.
*
* @var object
*/
private $vocabTags;
/**
* Category term without Sites to test with.
*
* @var object
*/
private $termCategoryWithoutSite;
/**
* Category term with one Site to test with.
*
* @var object
*/
private $termCategoryWithSite;
/**
* Category term with multiple Sites to test with.
*
* @var object
*/
private $termCategoryWithSites;
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => t('Site Taxonomy : API'),
'description' => t('Tests Site Taxonomy terms functionality.'),
'group' => t('Organic Groups Site Manager'),
);
}
/**
* {@inheritdoc}
*/
public function setUp() {
// Enable required modules.
$modules = array('og_sm_taxonomy');
parent::setUp($modules);
// Create the Site type.
$type = $this->ogSmCreateGroupNodeType();
og_sm_site_type_add($type);
$this->siteNode1 = $this->ogSmCreateGroup($type);
$this->siteNode2 = $this->ogSmCreateGroup($type);
// Create vocabularies.
$this->vocabCategories = $this->ogSmCreateGroupVocabulary('test_categories');
$this->vocabTags = $this->ogSmCreateGroupVocabulary('test_tags');
// Create terms.
$this->termCategoryWithoutSite = $this->ogSmCreateTerm(
$this->vocabCategories,
'category-without-sites'
);
$this->termCategoryWithSite = $this->ogSmCreateTerm(
$this->vocabCategories,
'category-with-site',
array($this->siteNode1)
);
$this->termCategoryWithSites = $this->ogSmCreateTerm(
$this->vocabCategories,
'category-with-sites',
array($this->siteNode1, $this->siteNode2)
);
}
/**
* Test the vocabulary API.
*/
public function testApi() {
// Test getting all the names.
$expected = array(
'test_categories' => 'test_categories',
'test_tags' => 'test_tags',
);
$this->assertEqual(
$expected,
og_sm_taxonomy_get_vocabulary_names(),
'All vocabularies with OG Audience field are listed.'
);
// Test loading all vocabularies.
$vocabs = og_sm_taxonomy_get_vocabularies();
$this->assertEqual(2, count($vocabs), 'There are 2 vocabularies with OG Audience field.');
// Test check if vocabulary has the OG Audience field.
$this->assertFalse(
og_sm_taxonomy_is_vocabulary('tags'),
'Tags vocabulary has no OG Audience field.'
);
$this->assertTrue(
og_sm_taxonomy_is_vocabulary($this->vocabCategories->machine_name),
'Test Categories has the OG Audience field.'
);
$this->assertTrue(
og_sm_taxonomy_is_vocabulary($this->vocabTags->machine_name),
'Test Categories has the OG Audience field.'
);
// Test getting the Sites from a Term.
$expected = array();
$this->assertEqual(
$expected,
og_sm_taxonomy_term_get_sites($this->termCategoryWithoutSite),
'Term without Sites.'
);
$expected = array($this->siteNode1->nid => $this->siteNode1);
$this->assertEqual(
$expected,
og_sm_taxonomy_term_get_sites($this->termCategoryWithSite),
'Term with one Site.'
);
$this->assertEqual(
$this->siteNode1,
og_sm_taxonomy_term_get_site($this->termCategoryWithSite),
'Single Site term returns the only Site it belongs to.'
);
$expected[$this->siteNode2->nid] = $this->siteNode2;
$this->assertEqual(
$expected,
og_sm_taxonomy_term_get_sites($this->termCategoryWithSites),
'Term with two Sites.'
);
$this->assertEqual(
$this->siteNode1,
og_sm_taxonomy_term_get_site($this->termCategoryWithSites),
'Multiple Site Term returns the first Site it belongs to.'
);
// Test checking if a Term is used within one or more Sites.
$this->assertFalse(
og_sm_taxonomy_term_is_site_term($this->termCategoryWithoutSite),
'Term is not member of any Site'
);
$this->assertTrue(
og_sm_taxonomy_term_is_site_term($this->termCategoryWithSite),
'Term is member of at least one Site'
);
// Test checking if a Term belongs to a given Site.
$this->assertFalse(
og_sm_taxonomy_term_is_site_member($this->termCategoryWithoutSite, $this->siteNode1),
'Term is not member of a Site'
);
$this->assertTrue(
og_sm_taxonomy_term_is_site_member($this->termCategoryWithSite, $this->siteNode1),
'Term is member of Site 1'
);
$this->assertFalse(
og_sm_taxonomy_term_is_site_member($this->termCategoryWithSite, $this->siteNode2),
'Term is not member of Site 2'
);
$this->assertTrue(
og_sm_taxonomy_term_is_site_member($this->termCategoryWithSites, $this->siteNode2),
'Term is member of Site 2'
);
}
/**
* Test the term_access query tag.
*/
public function testTermAccessQueryTag() {
// Create a base query which joins the taxonomy_term_data table and
// taxonomy_vocabulary to fetch tids.
$base_query = db_select('taxonomy_term_data', 'ttd')
->fields('ttd', array('tid'));
$base_query->innerJoin('taxonomy_vocabulary', 'tv', 'tv.vid = ttd.vid');
$base_query->addTag('term_access');
// Simple condition that filters on vid.
$query = clone $base_query;
$query->condition('tv.vid', $this->vocabCategories->vid);
$this->assertSelectQueryResultCount($this->siteNode1, $query, 2);
$this->assertSelectQueryResultCount($this->siteNode2, $query, 1);
// Filters on machine name.
$query = clone $base_query;
$query->condition('tv.machine_name', $this->vocabCategories->machine_name);
$this->assertSelectQueryResultCount($this->siteNode1, $query, 2);
$this->assertSelectQueryResultCount($this->siteNode2, $query, 1);
// Create a more complex condition with multiple child conditions.
$query = clone $base_query;
$db_and = db_and();
$db_and->condition('tv.machine_name', $this->vocabCategories->machine_name);
$db_and->condition('tv.module', 'taxonomy');
$query->condition($db_and);
$this->assertSelectQueryResultCount($this->siteNode1, $query, 2);
$this->assertSelectQueryResultCount($this->siteNode2, $query, 1);
}
/**
* Checks the number of rows in a select query within a site context.
*
* @param object $site
* The site node.
* @param SelectQuery $query
* The SelectQuery object.
* @param int $expected_count
* The expected result count.
*
* @return bool
* TRUE if the assertion succeeded, FALSE otherwise.
*/
public function assertSelectQueryResultCount($site, SelectQuery $query, $expected_count) {
$query = clone $query;
$this->ogSmSetOgContextToGroup($site);
return $this->assertEqual($query->execute()->rowCount(), $expected_count);
}
}
