ng_lightbox.test in NG Lightbox 7
NG Lightbox tests.
File
tests/ng_lightbox.testView source
<?php
/**
* @file
* NG Lightbox tests.
*/
class NgLightboxTest extends DrupalWebTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'NG Lightbox',
'group' => 'NG Lightbox',
'description' => 'Base tests for the NG Lightbox module.',
);
}
/**
* {@inheritdoc}
*/
public function setUp() {
variable_set('clean_url', TRUE);
parent::setUp(array(
'ng_lightbox',
'ng_lightbox_test',
));
}
/**
* Test the basic usage and settings.
*/
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.');
}
/**
* Test that the lightbox is disabled on admin paths.
*/
public function testAdminPathSetting() {
// Create an admin user and a test node.
$account = $this
->drupalCreateUser(array(
'bypass node access',
'access content overview',
'view the administration theme',
));
$this
->drupalLogin($account);
$node = $this
->drupalCreateNode();
variable_set('ng_lightbox_patterns', 'node/' . $node->nid . '/edit');
// Skip the admin paths, there should be no lightbox in the admin.
variable_set('ng_lightbox_skip_admin_paths', TRUE);
$this
->drupalGet('admin/content');
$anchor = $this
->xpath('//li[@class=:class]/a', array(
':class' => 'edit first',
));
$class = implode(' ', (array) $anchor[0]
->attributes()->class);
$this
->assertNotContains('ng-lightbox', $class, 'Edit link in admin does not have NG lightbox.');
// Now disable the setting and try again.
variable_set('ng_lightbox_skip_admin_paths', FALSE);
$this
->drupalGet('admin/content');
$anchor = $this
->xpath('//li[@class=:class]/a', array(
':class' => 'edit first',
));
$class = implode(' ', (array) $anchor[0]
->attributes()->class);
$this
->assertContains('ng-lightbox', $class, 'Edit link in admin is NG Lightbox enabled.');
}
/**
* Test the pattern matching for link paths.
*/
public function testPatternMatching() {
// Disable this since we'll technically be in the admin batch runner.
variable_set('ng_lightbox_skip_admin_paths', FALSE);
// Create a node to link to and add an alias for the node.
$alias = $this
->randomName();
$node1 = $this
->drupalCreateNode(array(
'path' => array(
'alias' => $alias,
),
));
// Only setting the aliased path should still match.
variable_set('ng_lightbox_patterns', $alias);
$this
->assertContains('ng-lightbox', l('Normal Path', 'node/' . $node1->nid));
// The uppercase path should still be matched for a lightbox.
$this
->assertContains('ng-lightbox', l('Uppercase Path', 'NODE/' . $node1->nid));
// The link with the path alias also works.
$this
->assertContains('ng-lightbox', l('Aliased Path', $alias));
// The link with the empty path should never be matched.
$this
->assertNotContains('ng-lightbox', l('Empty Path', ''));
}
/**
* We have a light-weight implementation of drupalPostAJAX() that works
* without requiring a form.
*
* @param string $path
* The Drupal path to post to.
* @param $post_array
* The array of post data.
*
* @return array
* The array of AJAX commands returned.
*/
protected function simplePostAJAX($path, $post_array) {
$out = $this
->curlExec(array(
CURLOPT_URL => url($path, array(
'absolute' => TRUE,
)),
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $post_array,
CURLOPT_HTTPHEADER => array(),
));
$this
->verbose('POST request to: ' . $path . '<hr />Ending URL: ' . $this
->getUrl() . '<hr />Fields: ' . highlight_string('<?php ' . var_export($post_array, TRUE), TRUE) . '<hr />' . $out);
return drupal_json_decode($out);
}
/**
* Asserts a string does exist in the haystack.
*
* @param string $needle
* The string to search for.
* @param string $haystack
* The string to search within.
* @param string $message
* The message to log.
*
* @return bool
* TRUE if it was found otherwise FALSE.
*/
protected function assertContains($needle, $haystack, $message = '') {
if (empty($message)) {
$message = t('%needle was found within %haystack', array(
'%needle' => $needle,
'%haystack' => $haystack,
));
}
return $this
->assertTrue(stripos($haystack, $needle) !== FALSE, $message);
}
/**
* Asserts a string does not exist in the haystack.
*
* @param string $needle
* The string to search for.
* @param string $haystack
* The string to search within.
* @param string $message
* The message to log.
*
* @return bool
* TRUE if it was not found otherwise FALSE.
*/
protected function assertNotContains($needle, $haystack, $message = '') {
if (empty($message)) {
$message = t('%needle was not found within %haystack', array(
'%needle' => $needle,
'%haystack' => $haystack,
));
}
return $this
->assertTrue(stripos($haystack, $needle) === FALSE, $message);
}
}
Classes
Name | Description |
---|---|
NgLightboxTest | @file NG Lightbox tests. |