You are here

authcache_panels_page_manager.test in Authenticated User Page Caching (Authcache) 7.2

Test cases for the Authcache PanelsPageManager module.

File

modules/authcache_panels_page_manager/authcache_panels_page_manager.test
View source
<?php

/**
 * @file
 * Test cases for the Authcache PanelsPageManager module.
 */

/**
 * Tests for markup substitution.
 */
class AuthcachePanelsPageManagerTest extends DrupalWebTestCase {
  protected $stubmod;
  protected $admin;
  protected $member;
  protected $node;

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Authcache Panels by Page Manager',
      'description' => 'Test markup substitution and fragment generation for Panels by Page Manager',
      'group' => 'Authcache Panels by Page Manager',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp(array(
      'authcache_panels_page_manager',
      'authcache_p13n_test',
      'php',
    ));
    $this->admin = $this
      ->drupalCreateUser(array(
      'use page manager',
      'administer page manager',
      'use ctools import',
      'use PHP for settings',
      'create article content',
    ));
    $this->member = $this
      ->drupalCreateUser();

    // Create node.
    $this->node = $this
      ->drupalCreateNode(array(
      'type' => 'article',
      'promote' => 1,
      'uid' => $this->admin->uid,
    ));

    // Setup authcache.
    variable_set('authcache_roles', array(
      DRUPAL_ANONYMOUS_RID => DRUPAL_ANONYMOUS_RID,
      DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
    ) + $this->member->roles);

    // HookStub.
    $this->stubmod = new ModuleStub('authcache_p13n_test');
  }

  /**
   * Test whether the given stub passes the invocation verifier.
   */
  protected function assertStub(HookStubProxy $stub, $verifier, $message = NULL) {
    $result = $stub
      ->verify($verifier, $error);
    if (!$message) {
      $message = t('Verify invocation of hook @hook.', array(
        '@hook' => $stub
          ->hookname(),
      ));
    }
    if (!$result && is_string($error)) {
      $message .= ' ' . $error;
    }
    $this
      ->assertTrue($result, $message);
  }

  /**
   * Test custom page manager page defined in database.
   */
  public function testCustomPageManagerPageDefinedInDatabase() {
    $this
      ->drupalLogin($this->admin);

    // Import stub-page.
    $edit = array(
      'object' => file_get_contents(drupal_get_path('module', 'authcache_panels_page_manager') . '/tests/custom-page.stub.txt'),
    );
    $this
      ->drupalPost('admin/structure/pages/import', $edit, t('Import'));
    $this
      ->drupalPost(NULL, array(), t('Save'));
    $this
      ->assertResponse(200);
    $this
      ->drupalLogout();
    authcache_p13n_request_router_rebuild();

    // Setup client and fragment stub.
    $client_info = array(
      'authcache_p13n_test' => array(
        'title' => t('Test Client'),
        'enabled' => TRUE,
      ),
    );
    $this->stubmod
      ->hook('authcache_p13n_client', $client_info);
    $substituted_markup = $this
      ->randomName(8);
    $fragment_stub = HookStub::on('theme_authcache_p13n_fragment__authcache_p13n_test', $substituted_markup);
    $this
      ->drupalLogin($this->member);
    $this
      ->drupalGet('node-x/' . $this->node->nid);
    $this
      ->assertResponse(200);

    // Ensure that the panels pane was NOT rendered but the substitute markup
    // instead.
    $this
      ->assertNoText('This fragment should be loaded via Ajax/ESI. Some context:');
    $this
      ->assertText($substituted_markup);
    $this
      ->assertStub($fragment_stub, HookStub::once());

    // Try to receive the fragment. Note: pane_1 is the machine-name it is part
    // of the exported stub-task.
    $request_id = 'frag/panels/pane-1';

    // FIXME: We should'nt need to mess around with $_GET here in order to
    // restore proper context for authcache_p13n_request_get_callback.
    $orig_q = $_GET['q'];
    $_GET['q'] = 'node-x/' . $this->node->nid;
    $url = authcache_p13n_request_get_callback($request_id, NULL);
    $_GET['q'] = $orig_q;
    $this
      ->assertTrue($url);
    $this
      ->drupalGet($GLOBALS['base_root'] . $url['path'], $url['options'], array(
      'X-Authcache: 1',
    ));
    $this
      ->assertText('This fragment should be loaded via Ajax/ESI. Some context:');
    $this
      ->assertText('Authors email: ' . $this->admin->mail);
    $this
      ->assertText('Logged in users email: ' . $this->member->mail);
  }

  /**
   * Test custom page manager page defined in code.
   */
  public function testCustomPageManagerPageDefinedInCode() {

    // Enable test module which provides hook_default_page_manager_pages().
    module_enable(array(
      'authcache_panels_page_manager_test',
    ));
    $this
      ->resetAll();
    authcache_p13n_request_router_rebuild();

    // Setup client and fragment stub.
    $client_info = array(
      'authcache_p13n_test' => array(
        'title' => t('Test Client'),
        'enabled' => TRUE,
      ),
    );
    $this->stubmod
      ->hook('authcache_p13n_client', $client_info);
    $substituted_markup = $this
      ->randomName(8);
    $fragment_stub = HookStub::on('theme_authcache_p13n_fragment__authcache_p13n_test', $substituted_markup);
    $this
      ->drupalLogin($this->member);
    $this
      ->drupalGet('node-x/' . $this->node->nid);
    $this
      ->assertResponse(200);

    // Ensure that the panels pane was NOT rendered but the substitute markup
    // instead.
    $this
      ->assertNoText('This fragment should be loaded via Ajax/ESI. Some context:');
    $this
      ->assertText($substituted_markup);
    $this
      ->assertStub($fragment_stub, HookStub::once());

    // Try to receive the fragment. Note: pane_1 is the machine-name it is part
    // of the exported stub-task.
    $request_id = 'frag/panels/pane-1';

    // FIXME: We should'nt need to mess around with $_GET here in order to
    // restore proper context for authcache_p13n_request_get_callback.
    $orig_q = $_GET['q'];
    $_GET['q'] = 'node-x/' . $this->node->nid;
    $url = authcache_p13n_request_get_callback($request_id, NULL);
    $_GET['q'] = $orig_q;
    $this
      ->assertTrue($url);
    $this
      ->drupalGet($GLOBALS['base_root'] . $url['path'], $url['options'], array(
      'X-Authcache: 1',
    ));
    $this
      ->assertText('This fragment should be loaded via Ajax/ESI. Some context:');
    $this
      ->assertText('Authors email: ' . $this->admin->mail);
    $this
      ->assertText('Logged in users email: ' . $this->member->mail);
  }

  /**
   * Test the behavior on empty content panes.
   */
  public function testEmptyContentPane() {
    $this
      ->drupalLogin($this->admin);

    // Enable the node page.
    $this
      ->drupalGet('admin/structure/pages');
    $this
      ->clickLink(t('Edit'), 4);

    // Import variant.
    $edit = array(
      'object' => file_get_contents(drupal_get_path('module', 'authcache_panels_page_manager') . '/tests/node-page-forced-node-42.stub.txt'),
    );
    $this
      ->drupalPost('admin/structure/pages/nojs/operation/node_view/actions/import', $edit, t('Update and save'));
    $this
      ->assertResponse(200);
    $this
      ->clickLink(t('Enable'));
    $this
      ->drupalPost(NULL, array(), t('Enable'));
    $this
      ->drupalLogout();
    authcache_p13n_request_router_rebuild();

    // Setup client and fragment stub.
    $client_info = array(
      'authcache_p13n_test' => array(
        'title' => t('Test Client'),
        'enabled' => TRUE,
      ),
    );
    $this->stubmod
      ->hook('authcache_p13n_client', $client_info);
    $substituted_markup = $this
      ->randomName(8);
    $fragment_stub = HookStub::on('theme_authcache_p13n_fragment__authcache_p13n_test', $substituted_markup);
    $this
      ->drupalLogin($this->member);
    $this
      ->drupalGet('node/' . $this->node->nid);
    $this
      ->assertResponse(200);

    // Test succeeds if there was no error during the rendering of the page.
    // However sadly, we are not able to attach the fragment loader when a
    // content pane did not produce any result. This might result in weird
    // results like outlined in https://drupal.org/node/2237659#comment-8681127.
    // Somebody has an idea on how to fix that?
    //
    // Correct but failing assertions:
    // $this->assertText($substituted_markup);
    // $this->assertStub($fragment_stub, HookStub::once());
    //
    // Wrong but working assertions:
    $this
      ->assertNoText($substituted_markup);
    $this
      ->assertStub($fragment_stub, HookStub::never());
  }

}

Classes

Namesort descending Description
AuthcachePanelsPageManagerTest Tests for markup substitution.