og_sm-8.x-1.0/og_sm_taxonomy/tests/OgSmTaxonomyTermTestCase.test

og_sm_taxonomy/tests/OgSmTaxonomyTermTestCase.test
<?php
/**
 * @file
 * Tests about the Site Taxonomies.
 */

/**
 * Tests about the Site Taxonomy Term.
 */
class OgSmTaxonomyTermTestCase extends OgSmWebTestCase {
  /**
   * Site Nodes to run the tests with.
   *
   * @var object
   */
  private $siteNodePublished1;
  private $siteNodePublished2;
  private $siteNodeNotPublished;

  /**
   * Vocabulary "categories" to run tests with.
   *
   * @var object
   */
  private $vocabCategories;

  /**
   * The admin user to test with.
   */
  private $userAdministrator;

  /**
   * Default user to test with.
   */
  private $userDefault;

  /**
   * Default Site user to test with.
   */
  private $siteUser;

  /**
   * Site user who can acces unpublished site.
   */
  private $siteUserWithAccess;

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => t('Site Taxonomy : Terms'),
      'description' => t('Tests Site Terms 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', 'og_sm_access');
    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 nodes.
    $this->siteNodePublished1   = $this->ogSmCreateGroup($type);
    $this->siteNodePublished2   = $this->ogSmCreateGroup($type);
    $this->siteNodeNotPublished = $this->ogSmCreateGroup($type, array('status' => 0));

    // Create site role who can access not-published site.
    $role_manager = og_role_create('role_group_manager', 'node', 0, $type);
    og_role_save($role_manager);
    og_role_grant_permissions($role_manager->rid, array(OG_SM_ACCESS_PERMISSION_SITE));

    // Create vocabularies.
    $this->vocabCategories = $this->ogSmCreateGroupVocabulary('test_categories');

    // Create global users.
    $this->userDefault = $this->drupalCreateUser(array('access content'));
    $this->userAdministrator = $this->ogSmCreateAdminUser();

    // Create site users.
    $this->siteUser = $this->ogSmCreateGroupUser(array('access content'), array($this->siteNodeNotPublished));
    $this->siteUserWithAccess = $this->ogSmCreateGroupUser(
      array('access content'),
      array($this->siteNodeNotPublished)
    );
    og_role_grant(
      'node',
      $this->siteNodeNotPublished->nid,
      $this->siteUserWithAccess->uid,
      $role_manager->rid
    );
  }

  /**
   * Check access to the taxonomy page.
   */
  public function testTaxonomyViewAccess() {
    $term_global = $this->ogSmCreateTerm(
      $this->vocabCategories,
      'term-global'
    );
    $path_global = 'taxonomy/term/' . $term_global->tid;
    $term_published = $this->ogSmCreateTerm(
      $this->vocabCategories,
      'term-published',
      array($this->siteNodePublished1)
    );
    $path_published = 'taxonomy/term/' . $term_published->tid;
    $term_not_published = $this->ogSmCreateTerm(
      $this->vocabCategories,
      'term-not-published',
      array($this->siteNodeNotPublished)
    );
    $path_not_published = 'taxonomy/term/' . $term_not_published->tid;

    // Test the access check function.
    $this->assertTrue(
      og_sm_taxonomy_term_view_access($term_global, $this->userDefault),
      'Term outside Site uses default access check (access content).'
    );
    $this->assertTrue(
      og_sm_taxonomy_term_view_access($term_published, $this->userDefault),
      'Everybody has access to term in published Site.'
    );
    $this->assertFalse(
      og_sm_taxonomy_term_view_access($term_not_published, $this->userDefault),
      'Users without proper permissions can not access terms in unpublished Site.'
    );

    // Test the menu alter.
    $this->drupalGet($path_global);
    $this->assertResponse(200, 'Everybody has access to terms outside Sites.');
    $this->assertText($term_global->name);

    $this->drupalGet($path_published);
    $this->assertResponse(200, 'Everybody has access to terms inside published Sites.');
    $this->assertText($term_published->name);

    $this->drupalGet($path_not_published);
    $this->assertResponse(403, 'Users without proper permissions have no access to terms inside not published Sites.');
    $this->assertNoText($term_not_published->name);

    $this->drupalLogin($this->userDefault);
    $this->drupalGet($path_not_published);
    $this->assertResponse(403, 'Global users without proper permissions have no access to terms inside not published Sites.');
    $this->assertNoText($term_not_published->name);

    $this->drupalLogin($this->siteUser);
    $this->drupalGet($path_not_published);
    $this->assertResponse(403, 'Site users without proper permissions have no access to terms inside not published Sites.');
    $this->assertNoText($term_not_published->name);

    $this->drupalLogin($this->siteUserWithAccess);
    $this->drupalGet($path_not_published);
    $this->assertResponse(200, 'Site users with proper permissions have access to terms inside not published Sites.');
    $this->assertText($term_not_published->name);

    $this->drupalLogin($this->userAdministrator);
    $this->drupalGet($path_not_published);
    $this->assertResponse(200, 'Platform administrator has access to all terms.');
    $this->assertText($term_not_published->name);
  }

  /**
   * Test the query alter based on the current Site context.
   */
  public function testTermQueryAlter() {
    $this->ogSmCreateTerm($this->vocabCategories, 'term-global');
    $term_site_1 = $this->ogSmCreateTerm($this->vocabCategories, 'term-site-1', array($this->siteNodePublished1));
    $this->ogSmCreateTerm($this->vocabCategories, 'term-site-2', array($this->siteNodePublished2));

    // No site context.
    $terms = taxonomy_get_tree($this->vocabCategories->vid);
    $this->assertEqual(3, count($terms), 'Global context returns all terms.');

    // Site 1 terms.
    $this->ogSmSetOgContextToGroup($this->siteNodePublished1);
    drupal_static_reset('taxonomy_get_tree');
    $terms = taxonomy_get_tree($this->vocabCategories->vid);
    $this->assertEqual(1, count($terms), 'Site context set to Site 1 limits the terms.');
    $this->assertEqual($term_site_1->name, $terms[0]->name, 'Only the Site 1 terms when in that Site context.');
  }

  /**
   * Test the helper to get all vocabulary terms for a given Site.
   */
  public function testGetVocabularyTermsBySite() {
    $this->ogSmCreateTerm($this->vocabCategories, 'term-global');
    $term_site_1 = $this->ogSmCreateTerm($this->vocabCategories, 'term-site-1', array($this->siteNodePublished1));
    $this->ogSmCreateTerm($this->vocabCategories, 'term-site-2', array($this->siteNodePublished2));

    $terms = og_sm_taxonomy_get_vocabulary_terms_by_site($this->vocabCategories, $this->siteNodePublished1);
    $this->assertEqual(1, count($terms), 'There is only 1 category term in Site 1');
    $this->assertEqual($term_site_1, array_shift($terms), 'The Site 1 category term is in the collection.');
  }

  /**
   * Test deleting all the Site terms when a site is deleted.
   */
  public function testDeleteAllSiteTermsWhenSiteIsDeleted() {
    $site_nid = $this->siteNodePublished1->nid;

    // Vocabularies.
    $categories = $this->vocabCategories;
    $tags = $this->ogSmCreateGroupVocabulary('test-tags');

    // Global terms.
    $global_category = $this->ogSmCreateTerm($categories, 'global-category');
    $global_tag = $this->ogSmCreateTerm($tags, 'global-tags');

    // Site terms.
    $sites = array($this->siteNodePublished1);
    $this->ogSmCreateTerm($categories, 'site-category', $sites);
    $this->ogSmCreateTerm($tags, 'site-tag', $sites);

    // Check created terms.
    $this->assertEqual(2, count(taxonomy_get_tree($categories->vid)), '1 global and 1 site category created.');
    $this->assertEqual(2, count(taxonomy_get_tree($tags->vid)), '1 global and 1 site tag created.');

    // Delete the Site.
    node_delete($this->siteNodePublished1->nid);
    $this->assertFalse(node_load($site_nid), 'Site is deleted.');

    // Check the terms.
    $category_terms = taxonomy_get_tree($categories->vid);
    $this->assertEqual(1, count($category_terms), 'The Site categories are deleted.');
    $this->assertEqual($global_category->tid, array_shift($category_terms)->tid, 'Global category is not deleted.');
    $tag_terms = taxonomy_get_tree($tags->vid);
    $this->assertEqual(1, count($tag_terms), 'The Site tags are deleted.');
    $this->assertEqual($global_tag->tid, array_shift($tag_terms)->tid, 'Global tag is not deleted.');
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc