You are here

class RecipeNodeAccessTest in Recipe 7

Same name and namespace in other branches
  1. 7.2 src/Tests/RecipeNodeAccessTest.php \Drupal\recipe\Tests\RecipeNodeAccessTest

Tests display of nodes in recipe lists for node access.

Hierarchy

Expanded class hierarchy of RecipeNodeAccessTest

File

src/Tests/RecipeNodeAccessTest.php, line 8

Namespace

Drupal\recipe\Tests
View source
class RecipeNodeAccessTest extends \DrupalWebTestCase {

  /**
   * A user with administrative privileges.
   *
   * @var \stdClass
   */
  protected $adminUser;

  /**
   * A node title.
   *
   * @var string
   */
  protected $nodeTitle;

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Recipe node access',
      'description' => 'Tests display of nodes in recipe lists for node access.',
      'group' => 'Recipe',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {

    // Enable modules required for testing.
    parent::setUp(array(
      'recipe',
      'node_access_test',
    ));

    // Create and log in the admin user with Recipe content permissions.
    $this->adminUser = $this
      ->drupalCreateUser(array(
      'create recipe content',
      'edit own recipe content',
      'administer blocks',
    ));
    $this
      ->drupalLogin($this->adminUser);

    // Set the variable to enable private nodes with node_access_test.
    node_access_rebuild();
    variable_set('node_access_test_private', TRUE);

    // Create a test recipe node.
    $this->nodeTitle = $this
      ->randomName(16);
    $edit = array(
      'title' => $this->nodeTitle,
      'recipe_description[value]' => $this
        ->randomName(16),
      'recipe_yield' => 1,
      'private' => TRUE,
    );
    $this
      ->drupalPost('node/add/recipe', $edit, 'Save');

    // Enable the Newest Recipes and Recipe Summary blocks.
    $edit = array(
      "blocks[recipe_recent][region]" => 'sidebar_first',
    );
    $this
      ->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  }

  /**
   * Tests node_access for nodes displayed in recipe lists.
   */
  public function testRecentRecipeBoxNodeAccess() {

    // Logout and assert that the anonymous user can't access the node.
    $this
      ->drupalLogout();
    $this
      ->drupalGet('node/1');
    $this
      ->assertResponse(403);

    // Assert that a link to the test recipe can't be seen at /recipe.
    $this
      ->drupalGet('recipe');
    $this
      ->assertNoLink($this->nodeTitle);

    // Log in as the admin_user and set the recipe node as public.
    $this
      ->drupalLogin($this->adminUser);
    $edit = array(
      'private' => FALSE,
    );
    $this
      ->drupalPost('node/1/edit', $edit, 'Save');

    // Logout and assert that the anonymous user can access the node.
    $this
      ->drupalLogout();
    $this
      ->drupalGet('node/1');
    $this
      ->assertResponse(200);

    // Logout and assert that two links to the test recipe can be seen at
    // /recipe. One should be from the /recipe page Recent Recipes box and the
    // other should be from the Latest Recipes block.
    $this
      ->drupalGet('recipe');
    $this
      ->assertLink($this->nodeTitle, 0);
    $this
      ->assertLink($this->nodeTitle, 1);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RecipeNodeAccessTest::$adminUser protected property A user with administrative privileges.
RecipeNodeAccessTest::$nodeTitle protected property A node title.
RecipeNodeAccessTest::getInfo public static function
RecipeNodeAccessTest::setUp public function
RecipeNodeAccessTest::testRecentRecipeBoxNodeAccess public function Tests node_access for nodes displayed in recipe lists.