og_sm-8.x-1.0/og_sm_taxonomy/tests/OgSmTaxonomyAdminTestCase.test
og_sm_taxonomy/tests/OgSmTaxonomyAdminTestCase.test
<?php
/**
* @file
* Tests about the Site Taxonomies.
*/
/**
* Tests about the Site Taxonomy API.
*/
class OgSmTaxonomyAdminTestCase extends OgSmWebTestCase {
/**
* Site Nodes to run the tests with.
*
* @var object
*/
private $siteNode;
private $siteNodeOther;
/**
* Vocabulary "categories" to run tests with.
*
* @var object
*/
private $vocabCategories;
/**
* Vocabulary "tags" to run tests with.
*
* @var object
*/
private $vocabTags;
/**
* The admin user to test with.
*/
private $userAdministrator;
/**
* Global user without any special permissions.
*/
private $userPermissionNone;
/**
* Global user with edit all permission.
*/
private $userPermissionAll;
/**
* The site user without taxonomy permissions.
*/
private $siteUserPermissionNone;
/**
* The site user with taxonomy administration permission.
*/
private $siteUserPermissionAll;
/**
* The site user with the permission to administer the categories.
*/
private $siteUserPermissionCategories;
/**
* The site user with the permission to administer the tags.
*/
private $siteUserPermissionTags;
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => t('Site Taxonomy : Administration'),
'description' => t('Tests Site Taxonomy administration functionality.'),
'group' => t('Organic Groups Site Manager'),
);
}
/**
* {@inheritdoc}
*/
public function setUp() {
// Enable required modules (we need context to filter the terms!).
$modules = array('og_sm_taxonomy', 'og_sm_context');
parent::setUp($modules);
// Setup OG context.
$context_enabled = array(
'og_sm_context_node' => new stdClass(),
'og_sm_context_admin' => new stdClass(),
);
variable_set('og_context_negotiation_group_context', $context_enabled);
$context_order = array(
'og_sm_context_node' => -50,
'og_sm_context_admin' => -49,
);
variable_set('og_context_providers_weight_group_context', $context_order);
// Create the Site type.
$type = $this->ogSmCreateGroupNodeType();
og_sm_site_type_add($type);
// Create the Site node.
$this->siteNode = $this->ogSmCreateGroup($type);
$this->siteNodeOther = $this->ogSmCreateGroup($type);
// Create vocabularies.
$this->vocabCategories = $this->ogSmCreateGroupVocabulary('test_categories');
$this->vocabTags = $this->ogSmCreateGroupVocabulary('test_tags');
// Create users.
$this->userAdministrator = $this->ogSmCreateAdminUser();
$this->userPermissionNone = $this->drupalCreateUser();
$this->userPermissionAll = $this->drupalCreateUser(array(OG_SM_TAXONOMY_PERM_ALL));
// Site user without permissions.
$this->siteUserPermissionNone = $this->ogSmCreateGroupUser(
array(),
array($this->siteNode)
);
// Site user with taxonomy administration permissions.
$site_role_all = og_role_create('site taxonomy all', 'node', 0, $type);
og_role_save($site_role_all);
og_role_grant_permissions($site_role_all->rid, array(OG_SM_TAXONOMY_OG_PERM_ALL));
$this->siteUserPermissionAll = $this->ogSmCreateGroupUser(
array(),
array($this->siteNode)
);
og_role_grant(
'node',
$this->siteNode->nid,
$this->siteUserPermissionAll->uid,
$site_role_all->rid
);
// Site user who can manage only categories (edit).
$site_role_categories = og_role_create('site taxonomy categories', 'node', 0, $type);
og_role_save($site_role_categories);
og_role_grant_permissions(
$site_role_categories->rid,
array(sprintf(OG_SM_TAXONOMY_OG_PERM_VOCAB_EDIT, 'test_categories'))
);
$this->siteUserPermissionCategories = $this->ogSmCreateGroupUser(
array(),
array($this->siteNode)
);
og_role_grant(
'node',
$this->siteNode->nid,
$this->siteUserPermissionCategories->uid,
$site_role_categories->rid
);
// Site user who can manage only tags (edit & delete).
$site_role_tags = og_role_create('site taxonomy tags', 'node', 0, $type);
og_role_save($site_role_tags);
og_role_grant_permissions(
$site_role_tags->rid,
array(
sprintf(OG_SM_TAXONOMY_OG_PERM_VOCAB_EDIT, 'test_tags'),
sprintf(OG_SM_TAXONOMY_OG_PERM_VOCAB_DELETE, 'test_tags'),
)
);
$this->siteUserPermissionTags = $this->ogSmCreateGroupUser(
array(),
array($this->siteNode)
);
og_role_grant(
'node',
$this->siteNode->nid,
$this->siteUserPermissionTags->uid,
$site_role_tags->rid
);
}
/**
* Test the taxonomy administration overview access.
*/
public function testTaxonomyAdministrationOverviewAccess() {
// Global Administrators should always have access.
$this->assertTrue(
og_sm_taxonomy_admin_vocabulary_overview_access(
$this->siteNode,
$this->userAdministrator
),
'Site Taxonomy administration overview : Global administrators have always access.'
);
// Global user without permissions should not have access.
$this->assertFalse(
og_sm_taxonomy_admin_vocabulary_overview_access(
$this->siteNode,
$this->userPermissionNone
),
'Site Taxonomy administration overview : Global user without permissions has no access.'
);
// Global user with the permission should have access.
$this->assertTrue(
og_sm_taxonomy_admin_vocabulary_overview_access(
$this->siteNode,
$this->userPermissionAll
),
'Site Taxonomy administration overview : Global user with permission has access.'
);
// Site users without permissions should not have access.
$this->assertFalse(
og_sm_taxonomy_admin_vocabulary_overview_access(
$this->siteNode,
$this->siteUserPermissionNone
),
'Site Taxonomy administration overview : Site user without permissions has no access.'
);
// Site users with all taxonomy permission should have access.
$this->assertTrue(
og_sm_taxonomy_admin_vocabulary_overview_access(
$this->siteNode,
$this->siteUserPermissionAll
),
'Site Taxonomy administration overview : Site user with edit all permissions has access.'
);
// Site users with at least one vocabulary edit access have access.
$this->assertTrue(
og_sm_taxonomy_admin_vocabulary_overview_access(
$this->siteNode,
$this->siteUserPermissionCategories
),
'Site Taxonomy administration overview : Site user with edit one of the vocabulary permissions has access.'
);
}
/**
* Test access to edit the terms of a vocabulary.
*/
public function testTaxonomyAdministrationVocabularyAccess() {
// Global Administrators should always have access.
$this->assertTrue(
og_sm_taxonomy_admin_vocabulary_access(
$this->siteNode,
$this->vocabCategories,
$this->userAdministrator
),
'Site Taxonomy edit vocabulary : Global administrators have always access.'
);
// Global user without permissions should not have access.
$this->assertFalse(
og_sm_taxonomy_admin_vocabulary_access(
$this->siteNode,
$this->vocabTags,
$this->userPermissionNone
),
'Site Taxonomy edit vocabulary : Global user without permissions has no access.'
);
// Global user with the permission should have access.
$this->assertTrue(
og_sm_taxonomy_admin_vocabulary_access(
$this->siteNode,
$this->vocabCategories,
$this->userPermissionAll
),
'Site Taxonomy edit vocabulary : Global user with permission has access.'
);
// Site users without permissions should not have access.
$this->assertFalse(
og_sm_taxonomy_admin_vocabulary_access(
$this->siteNode,
$this->vocabCategories,
$this->siteUserPermissionNone
),
'Site Taxonomy edit vocabulary : Site user without permissions has no access.'
);
// Site users with all taxonomy permission should have access.
$this->assertTrue(
og_sm_taxonomy_admin_vocabulary_access(
$this->siteNode,
$this->vocabTags,
$this->siteUserPermissionAll
),
'Site Taxonomy edit vocabulary : Site user with edit all permissions has access.'
);
// Site users with at taxonomy specific permission.
$this->assertTrue(
og_sm_taxonomy_admin_vocabulary_access(
$this->siteNode,
$this->vocabCategories,
$this->siteUserPermissionCategories
),
'Site Taxonomy edit vocabulary : Site user with vocabulary specific permissions has access to it.'
);
// Site user with taxonomy he has no access to.
$this->assertFalse(
og_sm_taxonomy_admin_vocabulary_access(
$this->siteNode,
$this->vocabTags,
$this->siteUserPermissionCategories
),
'Site Taxonomy edit vocabulary : Site user without vocabulary specific permissions has no access to it.'
);
}
/**
* Test access to delete a term.
*/
public function testTaxonomyAdministrationVocabularyDeleteAccess() {
// Add some terms.
$sites = array($this->siteNode);
$term_cat = $this->ogSmCreateTerm($this->vocabCategories, 'term_cat', $sites);
$term_tag = $this->ogSmCreateTerm($this->vocabTags, 'term_tag', $sites);
// Global Administrators should always have access.
$this->assertTrue(
og_sm_taxonomy_term_delete_access(
$term_cat,
$this->userAdministrator
),
'Site Taxonomy delete term : Global administrators have always access.'
);
// Global user without permissions should not have access.
$this->assertFalse(
og_sm_taxonomy_term_delete_access(
$term_cat,
$this->userPermissionNone
),
'Site Taxonomy delete term : Global user without permissions has no access.'
);
// Global user with the permission should have access.
$this->assertTrue(
og_sm_taxonomy_term_delete_access(
$term_cat,
$this->userPermissionAll
),
'Site Taxonomy delete term : Global user with permission has access.'
);
// Site users without permissions should not have access.
$this->assertFalse(
og_sm_taxonomy_term_delete_access(
$term_cat,
$this->siteUserPermissionNone
),
'Site Taxonomy delete term : Site user without permissions has no access.'
);
// Site users with all taxonomy permission should have access.
$this->assertTrue(
og_sm_taxonomy_term_delete_access(
$term_cat,
$this->siteUserPermissionAll
),
'Site Taxonomy delete term : Site user with edit all permissions has access.'
);
// Site users with taxonomy specific permission.
$this->assertTrue(
og_sm_taxonomy_term_delete_access(
$term_tag,
$this->siteUserPermissionTags
),
'Site Taxonomy delete term : Site user with vocabulary specific permissions has access to it.'
);
// Site user with taxonomy edit but no delete access.
$this->assertFalse(
og_sm_taxonomy_term_delete_access(
$term_cat,
$this->siteUserPermissionCategories
),
'Site Taxonomy delete term : Site user without vocabulary specific permissions has no access to it.'
);
}
/**
* Test the taxonomy overview page.
*/
public function testTaxonomyAdministrationOverviewPage() {
$path_overview = 'group/node/' . $this->siteNode->nid . '/admin/taxonomy';
$this->drupalLogin($this->userPermissionNone);
$this->drupalGet($path_overview);
$this->assertResponse(403, 'Site Taxonomy administration overview : No access without proper permissions.');
// User with access to administer all taxonomies should see all in the list.
$this->drupalLogin($this->userPermissionAll);
$this->drupalGet($path_overview);
$this->assertResponse(200, 'Site Taxonomy administration overview : Access when having edit all taxonomies permission.');
$this->assertText($this->vocabCategories->name, 'Category vocabulary is in the list.');
$this->assertText($this->vocabTags->name, 'Tags vocabulary is in the list.');
// User with access to only 1 vocabulary should not see other taxonomies.
$this->drupalLogin($this->siteUserPermissionCategories);
$this->drupalGet($path_overview);
$this->assertResponse(200, 'Site Taxonomy administration overview : Access when having edit at least one taxonomy permission.');
$this->assertText($this->vocabCategories->name, 'Category vocabulary is in the list.');
$this->assertNoText($this->vocabTags->name, 'Tags vocabulary is not in the list.');
}
/**
* Test the taxonomy list page.
*/
public function testTaxonomyAdministrationVocabularyPage() {
$path_categories = 'group/node/' . $this->siteNode->nid . '/admin/taxonomy/test_categories';
$this->drupalLogin($this->siteUserPermissionNone);
$this->drupalGet($path_categories);
$this->assertResponse(403, 'Site Taxonomy administration overview : No access without proper permissions.');
$this->drupalLogin($this->userAdministrator);
$this->drupalGet($path_categories);
$this->assertResponse(200, 'Site Taxonomy administration overview : Administrator has access to all vocabularies.');
// Check if the inline add link is pointing to the proper path.
$expected = sprintf(
'No terms available. <a href="/group/node/%d/admin/taxonomy/%s/add">Add term</a>.',
$this->siteNode->nid,
$this->vocabCategories->machine_name
);
$this->assertRaw(
$expected,
'Inline link to add a new term points to the path within the Site.'
);
// Add some category terms.
$sites = array($this->siteNode);
$this->ogSmCreateTerm($this->vocabCategories, 'term_outside');
$this->ogSmCreateTerm($this->vocabCategories, 'term_1', $sites);
$this->ogSmCreateTerm($this->vocabCategories, 'term_2', $sites);
$this->ogSmCreateTerm($this->vocabCategories, 'term_3', $sites);
// Check if the terms are listed.
$this->drupalGet($path_categories);
$this->assertNoText('term_outside', 'The no-site term is not in the list.');
$this->assertText('term_1', 'Term "term_1" is in the list.');
$this->assertText('term_2', 'Term "term_2" is in the list.');
$this->assertText('term_3', 'Term "term_3" is in the list.');
// Check if the add button is available.
$this->assertText('Add term');
}
/**
* Test ordering the taxonomy terms.
*/
public function testTaxonomyAdministrationVocabularyPageOrder() {
$path_categories = 'group/node/' . $this->siteNode->nid . '/admin/taxonomy/test_categories';
// Login to the platform.
$this->drupalLogin($this->userAdministrator);
// Add some terms.
$sites = array($this->siteNode);
$term_1 = $this->ogSmCreateTerm($this->vocabCategories, 'term_1', $sites);
$term_2 = $this->ogSmCreateTerm($this->vocabCategories, 'term_2', $sites);
$term_3 = $this->ogSmCreateTerm($this->vocabCategories, 'term_3', $sites);
// Change the weights.
$term_1->weight = 1;
taxonomy_term_save($term_1);
$term_2->weight = 1;
taxonomy_term_save($term_2);
$term_3->weight = 1;
taxonomy_term_save($term_3);
// Add some terms to another Site.
$term_other = $this->ogSmCreateTerm($this->vocabCategories, 'term_other', array($this->siteNodeOther));
$term_other->weight = 9999;
// Reset the order of the terms, we should return to the overview within the
// Site context.
$this->drupalPost($path_categories, array(), 'Reset to alphabetical');
$this->drupalPost(NULL, array(), t('Reset to alphabetical'));
$this->assertUrl(
$path_categories,
array(),
'The user is redirected back to the Site vocabulary terms overview page.'
);
// Check that we only changed the order of the Site terms, not of other
// Sites.
$term_1 = taxonomy_term_load($term_1->tid);
$this->assertEqual(0, $term_1->weight, 'Weight of term 1 is now 0.');
$term_2 = taxonomy_term_load($term_2->tid);
$this->assertEqual(0, $term_2->weight, 'Weight of term 2 is now 0.');
$term_3 = taxonomy_term_load($term_1->tid);
$this->assertEqual(0, $term_3->weight, 'Weight of term 3 is now 0.');
$term_other = taxonomy_term_load($term_other->tid);
$this->assertEqual(9999, $term_other->weight, 'Weight of term in other Site is not changed.');
// Open the reset order confirmation page, using the cancel button should
// redirect back to the Site vocabulary terms overview page.
$this->drupalPost($path_categories, array(), 'Reset to alphabetical');
$this->clickLink('Cancel');
$this->assertUrl(
$path_categories,
array(),
'The user is redirected back to the Site vocabulary terms overview page.'
);
}
/**
* Test adding a vocabulary term.
*/
public function testTaxonomyAdministrationTermAdd() {
$path_categories = 'group/node/' . $this->siteNode->nid . '/admin/taxonomy/test_categories';
$path_add = $path_categories . '/add';
// Login to the platform as user without proper permissions.
$this->drupalLogin($this->siteUserPermissionNone);
$this->drupalGet($path_add);
$this->assertResponse(403, 'Site Taxonomy term add : No access without proper permissions.');
$this->assertNoFieldById('edit-name', NULL, 'There should be no form on the page.');
// Login to the platform as user with proper permissions.
$this->drupalLogin($this->siteUserPermissionAll);
$this->drupalGet($path_add);
$this->assertResponse(200, 'Site Taxonomy term add : Administrator has access to all vocabularies.');
$this->assertFieldById('edit-name', NULL, 'Term field is on the page.');
// Post the form.
$data = array(
'name' => 'test_term_post',
'description[value]' => 'test_term_description',
'og_group_ref[und][0][default][]' => $this->siteNode->nid,
);
$this->drupalPost($path_add, $data, 'Save');
$this->drupalGet($path_categories);
$this->assertText(
'test_term_post',
'The new term is saved for the site.'
);
// Check if the term is created linked to the Site.
$term = taxonomy_get_term_by_name('test_term_post');
$term = array_shift($term);
$this->assertTrue(
og_sm_taxonomy_term_is_site_member($term, $this->siteNode),
'The term is a member of the Site.'
);
}
/**
* Test editing a vocabulary term.
*/
public function testTaxonomyAdministrationTermEdit() {
$sites = array($this->siteNode);
$term = $this->ogSmCreateTerm($this->vocabCategories, 'term_1', $sites);
$path_edit = 'taxonomy/term/' . $term->tid . '/edit';
// Not logged in.
$this->drupalGet($path_edit);
$this->assertResponse(403, 'Site Taxonomy term edit : No access without proper permissions.');
$this->assertNoFieldById('edit-description-value', NULL, 'There should be no form on the page.');
// No permission.
$this->drupalLogin($this->userPermissionNone);
$this->drupalGet($path_edit);
$this->assertResponse(403, 'Site Taxonomy term edit : No access without proper permissions.');
// Administrator has always access.
$this->drupalLogin($this->userAdministrator);
$this->drupalGet($path_edit);
$this->assertResponse(200, 'Site Taxonomy term edit : Platform administrator has access.');
$expected = $this->siteNode->title . ' (' . $this->siteNode->nid . ')';
$this->assertFieldById(
'edit-og-group-ref-und-0-admin-0-target-id',
$expected,
'Site field is filled in correctly.'
);
// Global user with proper permissions.
$this->drupalLogin($this->userPermissionAll);
$this->drupalGet($path_edit);
$this->assertResponse(200, 'Site Taxonomy term edit : Global user with edit all has access.');
// Site user without access.
$this->drupalLogin($this->siteUserPermissionNone);
$this->drupalGet($path_edit);
$this->assertResponse(403, 'Site Taxonomy term edit : Site user without proper permissions has no access.');
// Site user who can manage all.
$this->drupalLogin($this->siteUserPermissionAll);
$this->drupalGet($path_edit);
$this->assertResponse(200, 'Site Taxonomy term edit : Site user with edit all has access.');
// Site user with specific vocabulary.
$this->drupalLogin($this->siteUserPermissionCategories);
$this->drupalGet($path_edit);
$this->assertResponse(200, 'Site Taxonomy term edit : Site user with categories edit has access.');
// Site user without specific vocabulary access.
$this->drupalLogin($this->siteUserPermissionTags);
$this->drupalGet($path_edit);
$this->assertResponse(403, 'Site Taxonomy term edit : Site user without categories permissions has no access.');
}
/**
* Test deleting a Site taxonomy term.
*/
public function testTaxonomyAdministrationTermDelete() {
// Create some tags to test with.
$sites = array($this->siteNode);
$term_cat = $this->ogSmCreateTerm($this->vocabCategories, 'term_cat', $sites);
$path_cat_edit = 'taxonomy/term/' . $term_cat->tid . '/edit';
$term_tag = $this->ogSmCreateTerm($this->vocabTags, 'term_tag', $sites);
$path_tag_edit = 'taxonomy/term/' . $term_tag->tid . '/edit';
// Administrator has always access to the delete button.
$this->drupalLogin($this->userAdministrator);
$this->drupalGet($path_cat_edit);
$this->assertFieldById('edit-delete', t('Delete'), 'Site Taxonomy term delete : Administrator has always access.');
$this->drupalPost($path_cat_edit, array(), t('Delete'));
$this->assertText(
'Are you sure you want to delete the term ' . $term_cat->name,
'Site Taxonomy term delete : Administrator has access to delete confirmation form.'
);
// Global user with proper permissions.
$this->drupalLogin($this->userPermissionAll);
$this->drupalGet($path_cat_edit);
$this->assertFieldById('edit-delete', t('Delete'), 'Site Taxonomy term delete : Global user with edit all has access.');
$this->drupalPost($path_cat_edit, array(), t('Delete'));
$this->assertText(
'Are you sure you want to delete the term ' . $term_cat->name,
'Site Taxonomy term delete : Global user with edit all has access to delete confirmation form.'
);
// Site user who can manage all.
$this->drupalLogin($this->siteUserPermissionAll);
$this->drupalGet($path_cat_edit);
$this->assertFieldById('edit-delete', t('Delete'), 'Site Taxonomy term delete : Site user with edit all has access.');
$this->drupalPost($path_cat_edit, array(), t('Delete'));
$this->assertText(
'Are you sure you want to delete the term ' . $term_cat->name,
'Site Taxonomy term delete : Site user with edit all has access to delete confirmation form.'
);
// Site user with edit but no delete access.
$this->drupalLogin($this->siteUserPermissionCategories);
$this->drupalGet($path_cat_edit);
$this->assertNoFieldById('edit-delete', t('Delete'), 'Site Taxonomy term delete : Site user with only edit permission has no access.');
// Site user with edit and delete vocabulary term access.
$this->drupalLogin($this->siteUserPermissionTags);
$this->drupalGet($path_tag_edit);
$this->assertFieldById('edit-delete', t('Delete'), 'Site Taxonomy term delete : Site user with delete permission has access.');
$this->drupalPost($path_tag_edit, array(), t('Delete'));
$this->assertText(
'Are you sure you want to delete the term ' . $term_tag->name,
'Site Taxonomy term delete : Site user with delete permission has access to delete confirmation form.'
);
// Check redirect once the term is deleted.
$path_tags = 'group/node/' . $this->siteNode->nid . '/admin/taxonomy/test_tags';
$this->drupalPost(NULL, array(), t('Delete'));
$this->assertRaw(
t('Deleted term %name.', array('%name' => $term_tag->name)),
'Taxonomy term (tag) deleted.'
);
$this->assertUrl(
$path_tags,
array(),
'The user is redirected back to the Site Tags overview page.'
);
}
/**
* Test the alter of the global taxonomy administration.
*/
public function testTermAdministrationSiteInfo() {
$this->ogSmCreateTerm($this->vocabCategories, 'global-term');
$this->ogSmCreateTerm($this->vocabCategories, 'site-term', array($this->siteNode));
$this->drupalLogin($this->userAdministrator);
// Site info only shown on the Global overview.
$this->drupalGet('admin/structure/taxonomy/test_categories');
$this->assertRaw('>global-term</a>', 'Global terms do not have indication of the site.');
$this->assertRaw(
'>site-term</a> <small>(' . $this->siteNode->title . ')</small>',
'Site terms have indication of the site in the global term administration.'
);
// Site info not shown when in the Site terms administration.
$this->drupalGet('group/node/' . $this->siteNode->nid . '/admin/taxonomy/test_categories');
$this->assertRaw('>site-term</a>',
'Site terms are in the overview.'
);
$this->assertNoRaw(
'>site-term</a> <small>(' . $this->siteNode->title . ')</small>',
'Site terms have no indication of the Site in the Site term administration.'
);
}
}
