You are here

SearchByPageEnvironmentTest.php in Search by Page 8

Tests for the Search by Page module. By Jennifer Hodgdon of Poplar ProductivityWare, www.poplarware.com

File

tests/src/Functional/SearchByPageEnvironmentTest.php
View source
<?php

/**
 * @file
 * Tests for the Search by Page module.
 * By Jennifer Hodgdon of Poplar ProductivityWare, www.poplarware.com
 */
namespace Drupal\Tests\search_by_page\Functional;

use Drupal\Core\Language\Language;

/**
 * Basic search environments test.
 *
 * @group search_by_page
 */
class SearchByPageEnvironmentTest extends SearchByPageTesterTest {
  const LANGUAGE_NONE = Language::LANGCODE_NOT_SPECIFIED;
  public $superuser, $user1, $user2;
  public static $modules = [
    'search_by_page',
    'search_by_page_paths',
  ];
  public static function getInfo() {
    return [
      'name' => t('Search by Page Environments Tests'),
      'description' => t('Test that search environments can be set up.'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page',
      ),
    ];
  }
  public function setUp() {
    parent::setUp();
    $this
      ->setUpEnvironments();

    // Verify that environments are created
    $envs = search_by_page_list_environments();
    $this
      ->assertTrue(in_array($this->envid1, $envs), 'Environment 1 created');
    $this
      ->assertTrue(in_array($this->envid2, $envs), 'Environment 2 created');
    $perms = array_keys(search_by_page_permission());
    $this
      ->assertTrue(in_array('search page environment ' . $this->envinfo1['environment_name'], $perms), 'Environment 1 perm created');
    $this
      ->assertTrue(in_array('search page environment ' . $this->envinfo2['environment_name'], $perms), 'Environment 2 perm created');
    $this
      ->assertEqual('search page environment ' . $this->envinfo1['environment_name'], $this
      ->searchPerm($this->envinfo1), 'searchPerm function working');
    $this->superuser = $this
      ->drupalCreateUser([
      'administer blocks',
      'access administration pages',
      'administer search',
      'administer search by page',
      'search content',
      $this
        ->searchPerm($this->envinfo1),
      $this
        ->searchPerm($this->envinfo2),
      'administer site configuration',
      'administer permissions',
    ]);
    $this->user1 = $this
      ->drupalCreateUser([
      'search content',
      $this
        ->searchPerm($this->envinfo1),
    ]);
    $this->user2 = $this
      ->drupalCreateUser([
      'search content',
      $this
        ->searchPerm($this->envinfo2),
    ]);
  }

  /**
   * Tests that environment settings are working.
   */
  public function testEnvironments() {

    // Test that user1 and user2 can only see their pages.
    // And that the pages have correct text on them.
    // User 1 - has permission for env 1 only.
    $this
      ->drupalLogin($this->user1);
    $this
      ->drupalGet($this->envinfo1['page_path']);

    // The assertTitle function is lame, because it includes the site name...
    $this
      ->assertTrue(strstr(current($this
      ->xpath('//title')), $this->envinfo1['page_title']), 'Page title contains correct string');
    $this
      ->assertText($this->envinfo1['page_title'], 'Page title is visible');
    $this
      ->assertText($this->envinfo1['field_label'], 'Field label is correct');
    $this
      ->assertRaw($this->envinfo1['button_label'], 'Button label is correct');
    $this
      ->drupalGet($this->envinfo2['page_path']);
    $this
      ->assertText('Access denied', 'Access denied to user without permission');

    // User 2 - has permission for env 2 only
    $this
      ->drupalLogin($this->user2);
    $this
      ->drupalGet($this->envinfo2['page_path']);
    $this
      ->assertTrue(strstr(current($this
      ->xpath('//title')), $this->envinfo2['page_title']), 'Page title contains correct string');
    $this
      ->assertText($this->envinfo2['page_title'], 'Page title is visible');
    $this
      ->assertText($this->envinfo2['field_label'], 'Field label is correct');
    $this
      ->assertRaw($this->envinfo2['button_label'], 'Button label is correct');
    $this
      ->drupalGet($this->envinfo1['page_path']);
    $this
      ->assertText(t('denied'), 'Access denied to user without permission');

    // Test that blocks exist and have right titles when displayed.
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/structure/block');
    $this
      ->assertText(t('Search by page @env', [
      '@env' => $this->envinfo1['environment_name'],
    ]), "Block 1 exists");
    $this
      ->assertText(t('Search by page @env', [
      '@env' => $this->envinfo1['environment_name'],
    ]), "Block 2 exists");

    // Turn on both blocks.
    $this
      ->drupalPostForm('admin/structure/block', [
      'blocks[search_by_page_' . $this->envid1 . '][region]' => 'content',
      'blocks[search_by_page_' . $this->envid2 . '][region]' => 'content',
    ], 'Save blocks');

    // Verify block titles appear.
    $this
      ->drupalGet('<front>');
    $this
      ->assertText($this->envinfo1['block_title'], 'Block title is correct');
    $this
      ->assertText($this->envinfo2['block_title'], 'Block title is correct');
  }

}

Classes

Namesort descending Description
SearchByPageEnvironmentTest Basic search environments test.