You are here

public function NgLightboxTest::testNgLightbox in NG Lightbox 7

Test the basic usage and settings.

File

tests/ng_lightbox.test, line 32
NG Lightbox tests.

Class

NgLightboxTest
@file NG Lightbox tests.

Code

public function testNgLightbox() {
  $title = $this
    ->randomName();
  $node = $this
    ->drupalCreateNode(array(
    'promote' => 1,
    'title' => $title,
  ));
  $latin_title = 'my stupid random title';

  //$this->randomName();
  $latin_path = 'йцйцйц';
  $latin_node = $this
    ->drupalCreateNode(array(
    'promote' => 1,
    'title' => $latin_title,
    'path' => array(
      'alias' => $latin_path,
    ),
  ));

  // Go to the homepage which will link to the new nodes.
  $this
    ->drupalGet('node');

  // Get the read more link for the node and assert no ng-lightbox class.
  $anchor = $this
    ->xpath('//li[@class=:class]/a', array(
    ':class' => 'node-readmore first',
  ));
  $class = implode(' ', (array) $anchor[0]
    ->attributes()->class);
  $this
    ->assertNotContains('ng-lightbox', $class, 'Read more link does not have ng-lightbox class');

  // Enable the lightbox for our new node.
  variable_set('ng_lightbox_patterns', 'node/' . $node->nid . "\n{$latin_path}");

  // Refresh the page and assert the read more link now has the ng-lightbox
  // class.
  $this
    ->drupalGet($this
    ->getUrl());
  $anchor = $this
    ->xpath('//li[@class=:class]/a', array(
    ':class' => 'node-readmore first',
  ));
  $class = implode(' ', (array) $anchor[0]
    ->attributes()->class);
  $this
    ->assertContains('ng-lightbox', $class, 'Read more contains the ng-lightbox class');

  // Assert we have the node title for a valid node.
  global $base_path;
  $node_path = $base_path . 'node/' . $node->nid;
  $commands = $this
    ->simplePostAJAX('ng-lightbox', array(
    'ng_lightbox_path' => $node_path,
  ));
  $this
    ->assertContains($title, $commands[1]['data'], 'Valid node title is returned');

  // Assert paths with latin characters work.
  $commands = $this
    ->simplePostAJAX('ng-lightbox', array(
    'ng_lightbox_path' => $base_path . urlencode($latin_path),
  ));
  $this
    ->assertContains($latin_title, $commands[1]['data'], 'Valid node title is returned');

  // Test that is also works with clean_url switched off.
  variable_set('clean_url', FALSE);
  $commands = $this
    ->simplePostAJAX('ng-lightbox', array(
    'ng_lightbox_path' => $base_path . '?q=node/' . $node->nid,
  ));
  $this
    ->assertContains($title, $commands[1]['data'], 'Valid node title is returned');
  variable_set('clean_url', TRUE);

  // Assert a page which does not exist.
  $commands = $this
    ->simplePostAJAX('ng-lightbox', array(
    'ng_lightbox_path' => $base_path . 'node/doesn-not-exist',
  ));
  $this
    ->assertContains('Page not found', $commands[1]['data'], 'Page was a 404');

  // Assert a page we don't have permission to view is access denied.
  $node->status = 0;
  node_save($node);
  $commands = $this
    ->simplePostAJAX('ng-lightbox', array(
    'ng_lightbox_path' => $node_path,
  ));
  $this
    ->assertContains('Access Denied', $commands[1]['data'], 'Page was access denied');

  // Login to try some privileged actions.
  $admin_user = $this
    ->drupalCreateUser(array(
    'access administration pages',
    'access content overview',
    'administer nodes',
    'bypass node access',
  ));
  $this
    ->drupalLogin($admin_user);

  // Assert a form and that the #action is set correctly.
  $commands = $this
    ->simplePostAJAX('ng-lightbox', array(
    'ng_lightbox_path' => $node_path . '/edit',
  ));
  $this
    ->assertContains('action="' . $base_path . 'node/1/edit"', $commands[1]['data'], 'Form has a valid form action.');
  $this
    ->assertContains('page_node_form', $commands[1]['data'], 'Node edit form returned.');

  // Ensure the query params are preserved to the lightbox request.
  $random_1 = $this
    ->randomName();
  $random_2 = $this
    ->randomName();
  $commands = $this
    ->simplePostAJAX('ng-lightbox', array(
    'ng_lightbox_path' => $base_path . "ng-lightbox-test?p1={$random_1}&p2={$random_2}",
  ));
  $this
    ->assertContains('[p1] => ' . $random_1, $commands[1]['data'], 'Query params are preserved.');
  $this
    ->assertContains('[p2] => ' . $random_2, $commands[1]['data'], 'Query params are preserved.');
}