name = $this->randomName(); $vocabulary->description = $this->randomName(); $vocabulary->machine_name = drupal_strtolower($this->randomName()); $vocabulary->help = ''; $vocabulary->nodes = array('article' => 'article'); $vocabulary->weight = mt_rand(0, 10); taxonomy_vocabulary_save($vocabulary); return $vocabulary; } /** * Returns a new term with random properties in vocabulary $vid. */ protected function createTerm($vocabulary) { $term = new stdClass(); $term->name = $this->randomName(); $term->description = $this->randomName(); // Use the first available text format. $term->format = db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField(); $term->vid = $vocabulary->vid; taxonomy_term_save($term); return $term; } } /** * Tests the mechanism preventing terms from being unpublished. */ class TermstatusSafeguardTestCase extends TermstatusWebTestCase { /** * The installation profile to use with this test class. * * @var string */ protected $profile = 'testing'; /** * Enable the taxonomy module. */ public function setUp() { parent::setUp(array('taxonomy')); } /** * Return info about test case. */ public static function getInfo() { return array( 'name' => 'First Use Safeguard', 'description' => 'Tests the mechanism preventing terms from being unpublished after the module is enabled.', 'group' => 'Taxonomy Term Status', ); } /** * Tests the first-use-safeguard mechanism. */ public function testFirstUseSafeguard() { $vocabulary = $this->createVocabulary(); $term = $this->createTerm($vocabulary); $this->drupalGet('taxonomy/term/' . $term->tid); $this->assertResponse(200, 'Taxonomy term page should be accessible.'); $this->assertText($term->name, 'Page should contain the term name.'); // Make sure variable termstatus_enable does not exist. variable_del('termstatus_enable'); module_enable(array('termstatus')); // Reset/rebuild all data structures after enabling the modules. $this->resetAll(); // Safeguard against unpublished terms: Variable termstatus_enable should // be set to FALSE. $this->assertIdentical(FALSE, variable_get('termstatus_enable'), 'Variable termstatus_enable should be set to false after module is enabled'); // Ensure that the term is still accessible - even if its publishing state // has not changed yet. $this->drupalGet('taxonomy/term/' . $term->tid); $this->assertResponse(200, 'Taxonomy term page should still be accessible after module was enabled.'); $this->assertText($term->name, 'Page should contain the term name.'); // Ensure that a warning is reported through hook_requirements. module_load_install('termstatus'); $termstatus_requirements = module_invoke('termstatus', 'requirements', 'runtime'); $this->assertEqual(1, count($termstatus_requirements), 'Should report one requirement record'); $req = reset($termstatus_requirements); $this->assertEqual($req['severity'], REQUIREMENT_WARNING, 'Severity of requirement record should be "warning".'); // Turn on the variable and test whether the requirements are updated. variable_set('termstatus_enable', TRUE); $termstatus_requirements = module_invoke('termstatus', 'requirements', 'runtime'); $this->assertEqual(1, count($termstatus_requirements), 'Should report one requirement record'); $req = reset($termstatus_requirements); $this->assertEqual($req['severity'], REQUIREMENT_OK, 'Severity of requirement record should be "ok" after "termstatus_enable" variable has been manipulated.'); } /** * Tests the first-use-safeguard mechanism when no terms are present. */ public function testFirstUseSafeguardNoTerms() { // Make sure variable termstatus_enable does not exist. variable_del('termstatus_enable'); module_enable(array('termstatus')); // Reset/rebuild all data structures after enabling the modules. $this->resetAll(); // Safeguard against unpublished terms: Variable termstatus_enable should // be set to TRUE when no terms were present at the point of time, the // module has been enabled. $this->assertIdentical(TRUE, variable_get('termstatus_enable'), 'Variable termstatus_enable should be set to true after module is enabled, when no terms are present in the database.'); } } /** * Tests the administrative user interface. */ class TermstatusAdminTestCase extends TermstatusWebTestCase { /** * Return info about test case. */ public static function getInfo() { return array( 'name' => 'Administrative Interface', 'description' => 'Tests the administrative user interface.', 'group' => 'Taxonomy Term Status', ); } /** * Return info about test case. */ public function setUp() { parent::setUp(array('termstatus')); variable_set('termstatus_enable', TRUE); } /** * Test alterations of vocabulary edit form. */ public function testPerVocabularyDefaults() { $admin = $this->drupalCreateUser(array('administer taxonomy')); $this->drupalLogin($admin); // Test default behavior (default: published). variable_del('termstatus_default_tags'); $this->drupalGet('admin/structure/taxonomy/tags/edit'); $this->assertFieldChecked('edit-termstatus-default-status', 'Default publishing status should be enabled by default.'); $this->drupalGet('admin/structure/taxonomy/tags/add'); $this->assertFieldChecked('edit-status', 'Publishing status on term should be checked.'); // Set default to published. $edit = array('termstatus_default_status' => TRUE); $this->drupalPost('admin/structure/taxonomy/tags/edit', $edit, t('Save')); $this->assertEqual(1, variable_get('termstatus_default_tags'), 'Default publishing status for vocabulary should be saved in the proper variable when enabled.'); $this->drupalGet('admin/structure/taxonomy/tags/add'); $this->assertFieldChecked('edit-status', 'Publishing status on term should be checked.'); // Set default to unpublished. $edit = array('termstatus_default_status' => FALSE); $this->drupalPost('admin/structure/taxonomy/tags/edit', $edit, t('Save')); $this->assertEqual(0, variable_get('termstatus_default_tags'), 'Default publishing status for vocabulary should be saved in the proper variable when disabled.'); $this->drupalGet('admin/structure/taxonomy/tags/add'); $this->assertNoFieldChecked('edit-status', 'Publishing status on term should not be checked.'); } /** * Test alterations of term edit form. */ public function testAddEditTermPublishingStatus() { $admin = $this->drupalCreateUser(array('administer taxonomy')); $this->drupalLogin($admin); // Add published term. $name = $this->randomName(8); $edit = array( 'name' => $name, 'status' => TRUE, ); $this->drupalPost('admin/structure/taxonomy/tags/add', $edit, t('Save')); $terms = taxonomy_get_term_by_name($name, 'tags'); $this->assertEqual(1, count($terms), 'A term has been created'); $term = reset($terms); $this->assertTrue($term->status, 'The status of the term is set to published when added.'); // Edit published term and depublish it. $edit = array( 'status' => FALSE, ); $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save')); $term = entity_load_unchanged('taxonomy_term', $term->tid); $this->assertFalse($term->status, 'The status of the term is set to unpublished after edit.'); // Add unpublished term. $name = $this->randomName(8); $edit = array( 'name' => $name, 'status' => FALSE, ); $this->drupalPost('admin/structure/taxonomy/tags/add', $edit, t('Save')); $terms = taxonomy_get_term_by_name($name, 'tags'); $this->assertEqual(1, count($terms), 'A term has been created'); $term = reset($terms); $this->assertFalse($term->status, 'The status of the term is set to unpublished'); // Edit unpublished term and publish it. $edit = array( 'status' => TRUE, ); $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save')); $term = entity_load_unchanged('taxonomy_term', $term->tid); $this->assertTrue($term->status, 'The status of the term is set to published after edit.'); } } /** * Tests the access control features. */ class TermstatusAccessTestCase extends TermstatusWebTestCase { protected $vocabulary; protected $publishedTerm; protected $unpublishedTerm; /** * Return info about test case. */ public static function getInfo() { return array( 'name' => 'Access control', 'description' => 'Tests access to unpublished terms', 'group' => 'Taxonomy Term Status', ); } /** * Return info about test case. */ public function setUp() { parent::setUp(array('termstatus', 'termstatus_test')); variable_set('termstatus_enable', TRUE); $this->vocabulary = taxonomy_vocabulary_machine_name_load('tags'); variable_set('termstatus_default_tags', 1); $this->publishedTerm = $this->createTerm($this->vocabulary); variable_set('termstatus_default_tags', 0); $this->unpublishedTerm = $this->createTerm($this->vocabulary); } /** * Test that access to unpublished terms is denied for guest users. */ public function testGuestAccessToUnpublishedTerms() { $this->drupalGet('taxonomy/term/' . $this->publishedTerm->tid); $this->assertResponse(200, 'Anonymous user can access published term'); $this->drupalGet('taxonomy/term/' . $this->unpublishedTerm->tid); $this->assertResponse(404, 'Anonymous user cannot access unpublished term'); $this->drupalGet('termstatus-test-term-list/' . $this->vocabulary->vid); $this->assertLinkByHref('taxonomy/term/' . $this->publishedTerm->tid, 0, 'Published term is in term listing for anonymous user'); $this->assertNoLinkByHref('taxonomy/term/' . $this->unpublishedTerm->tid, 0, 'Unpublished term is not in term listing for anonymous user'); } /** * Test that access to unpublished terms is only granted to privileged users. */ public function testPrivilegedAccessToUnpublishedTerms() { $editor = $this->drupalCreateUser(array('view unpublished taxonomy terms')); $this->drupalLogin($editor); $this->drupalGet('taxonomy/term/' . $this->publishedTerm->tid); $this->assertResponse(200, 'Privileged user can access published term'); $this->drupalGet('taxonomy/term/' . $this->unpublishedTerm->tid); $this->assertResponse(200, 'Privileged user can access unpublished term'); $this->drupalGet('termstatus-test-term-list/' . $this->vocabulary->vid); $this->assertLinkByHref('taxonomy/term/' . $this->publishedTerm->tid, 0, 'Published term is in term listing for privileged user'); $this->assertLinkByHref('taxonomy/term/' . $this->unpublishedTerm->tid, 0, 'Unpublished term is in term listing for privileged user'); $admin = $this->drupalCreateUser(array('administer taxonomy')); $this->drupalLogin($admin); $this->drupalGet('taxonomy/term/' . $this->publishedTerm->tid); $this->assertResponse(200, 'Admin can access published term'); $this->drupalGet('taxonomy/term/' . $this->unpublishedTerm->tid); $this->assertResponse(200, 'Admin can access unpublished term'); $this->drupalGet('termstatus-test-term-list/' . $this->vocabulary->vid); $this->assertLinkByHref('taxonomy/term/' . $this->publishedTerm->tid, 0, 'Published term is in term listing for admin'); $this->assertLinkByHref('taxonomy/term/' . $this->unpublishedTerm->tid, 0, 'Unpublished term is in term listing for admin'); } /** * Test that api function for unpublished terms. */ public function testAPIFunctionForUnpublishedTerms() { $guest = drupal_anonymous_user(); $result = termstatus_term_access($this->publishedTerm, $guest); $this->assertTrue($result, 'Anonymous is granted access to published term'); $result = termstatus_term_access($this->unpublishedTerm, $guest); $this->assertFalse($result, 'Anonymous is denied access to unpublished term'); $editor = $this->drupalCreateUser(array('view unpublished taxonomy terms')); $result = termstatus_term_access($this->publishedTerm, $editor); $this->assertTrue($result, 'Privileged user is granted access to published term'); $result = termstatus_term_access($this->unpublishedTerm, $editor); $this->assertTrue($result, 'Privileged user is granted access to unpublished term'); $admin = $this->drupalCreateUser(array('administer taxonomy')); $result = termstatus_term_access($this->publishedTerm, $admin); $this->assertTrue($result, 'Admin is granted access to published term'); $result = termstatus_term_access($this->unpublishedTerm, $admin); $this->assertTrue($result, 'Admin is granted access to unpublished term'); } /** * Test visibility of unpublished terms in menu. */ public function testVisibilityOfMenuLinksForPrivilegedUser() { $published_menu_link = array( 'link_path' => 'taxonomy/term/' . $this->publishedTerm->tid, 'link_title' => $this->publishedTerm->name, 'menu_name' => 'main-menu', ); $unpublished_menu_link = array( 'link_path' => 'taxonomy/term/' . $this->unpublishedTerm->tid, 'link_title' => $this->unpublishedTerm->name, 'menu_name' => 'main-menu', ); menu_link_save($published_menu_link); menu_link_save($unpublished_menu_link); $editor = $this->drupalCreateUser(array('view unpublished taxonomy terms')); $this->drupalLogin($editor); $this->drupalGet('node'); $this->assertLinkByHref('taxonomy/term/' . $this->publishedTerm->tid, 0, 'Menu link for published term is visible'); $this->assertLinkByHref('taxonomy/term/' . $this->unpublishedTerm->tid, 0, 'Menu link for unpublished term is visible'); $admin = $this->drupalCreateUser(array('administer taxonomy')); $this->drupalLogin($admin); $this->drupalGet('node'); $this->assertLinkByHref('taxonomy/term/' . $this->publishedTerm->tid, 0, 'Menu link for published term is visible'); $this->assertLinkByHref('taxonomy/term/' . $this->unpublishedTerm->tid, 0, 'Menu link for unpublished term is visible'); } /** * Test visibility of unpublished terms in menu. */ public function testVisibilityOfMenuLinksForGuest() { $published_menu_link = array( 'link_path' => 'taxonomy/term/' . $this->publishedTerm->tid, 'link_title' => $this->publishedTerm->name, 'menu_name' => 'main-menu', ); $unpublished_menu_link = array( 'link_path' => 'taxonomy/term/' . $this->unpublishedTerm->tid, 'link_title' => $this->unpublishedTerm->name, 'menu_name' => 'main-menu', ); menu_link_save($published_menu_link); menu_link_save($unpublished_menu_link); $this->drupalGet('node'); $this->assertLinkByHref('taxonomy/term/' . $this->publishedTerm->tid, 0, 'Menu link for published term is visible'); $this->assertNoLinkByHref('taxonomy/term/' . $this->unpublishedTerm->tid, 0, 'Menu link for unpublished term is not visible'); } }