Commit fbe34afe authored by catch's avatar catch

Issue #1598564 by Niklas Fiekas: Convert common.test to PSR-0.

parent f6f33a19
<?php
/**
* @file
* Definition of Drupal\system\Tests\Common\AddFeedTest.
*/
namespace Drupal\system\Tests\Common;
use Drupal\simpletest\WebTestBase;
/**
* Basic tests for drupal_add_feed().
*/
class AddFeedTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'drupal_add_feed() tests',
'description' => 'Make sure that drupal_add_feed() works correctly with various constructs.',
'group' => 'Common',
);
}
/**
* Test drupal_add_feed() with paths, URLs, and titles.
*/
function testBasicFeedAddNoTitle() {
$path = $this->randomName(12);
$external_url = 'http://' . $this->randomName(12) . '/' . $this->randomName(12);
$fully_qualified_local_url = url($this->randomName(12), array('absolute' => TRUE));
$path_for_title = $this->randomName(12);
$external_for_title = 'http://' . $this->randomName(12) . '/' . $this->randomName(12);
$fully_qualified_for_title = url($this->randomName(12), array('absolute' => TRUE));
// Possible permutations of drupal_add_feed() to test.
// - 'input_url': the path passed to drupal_add_feed(),
// - 'output_url': the expected URL to be found in the header.
// - 'title' == the title of the feed as passed into drupal_add_feed().
$urls = array(
'path without title' => array(
'input_url' => $path,
'output_url' => url($path, array('absolute' => TRUE)),
'title' => '',
),
'external url without title' => array(
'input_url' => $external_url,
'output_url' => $external_url,
'title' => '',
),
'local url without title' => array(
'input_url' => $fully_qualified_local_url,
'output_url' => $fully_qualified_local_url,
'title' => '',
),
'path with title' => array(
'input_url' => $path_for_title,
'output_url' => url($path_for_title, array('absolute' => TRUE)),
'title' => $this->randomName(12),
),
'external url with title' => array(
'input_url' => $external_for_title,
'output_url' => $external_for_title,
'title' => $this->randomName(12),
),
'local url with title' => array(
'input_url' => $fully_qualified_for_title,
'output_url' => $fully_qualified_for_title,
'title' => $this->randomName(12),
),
);
foreach ($urls as $description => $feed_info) {
drupal_add_feed($feed_info['input_url'], $feed_info['title']);
}
$this->drupalSetContent(drupal_get_html_head());
foreach ($urls as $description => $feed_info) {
$this->assertPattern($this->urlToRSSLinkPattern($feed_info['output_url'], $feed_info['title']), t('Found correct feed header for %description', array('%description' => $description)));
}
}
/**
* Create a pattern representing the RSS feed in the page.
*/
function urlToRSSLinkPattern($url, $title = '') {
// Escape any regular expression characters in the url ('?' is the worst).
$url = preg_replace('/([+?.*])/', '[$0]', $url);
$generated_pattern = '%<link +rel="alternate" +type="application/rss.xml" +title="' . $title . '" +href="' . $url . '" */>%';
return $generated_pattern;
}
}
<?php
/**
* @file
* Definition of Drupal\system\Tests\Common\AlterTest.
*/
namespace Drupal\system\Tests\Common;
use Drupal\simpletest\WebTestBase;
use stdClass;
/**
* Tests alteration of arguments passed to drupal_alter().
*/
class AlterTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Alter hook functionality',
'description' => 'Tests alteration of arguments passed to drupal_alter().',
'group' => 'Common',
);
}
function setUp() {
parent::setUp(array('block', 'common_test'));
}
function testDrupalAlter() {
// This test depends on Bartik, so make sure that it is always the current
// active theme.
global $theme, $base_theme_info;
$theme = 'bartik';
$base_theme_info = array();
$array = array('foo' => 'bar');
$entity = new stdClass();
$entity->foo = 'bar';
// Verify alteration of a single argument.
$array_copy = $array;
$array_expected = array('foo' => 'Drupal theme');
drupal_alter('drupal_alter', $array_copy);
$this->assertEqual($array_copy, $array_expected, t('Single array was altered.'));
$entity_copy = clone $entity;
$entity_expected = clone $entity;
$entity_expected->foo = 'Drupal theme';
drupal_alter('drupal_alter', $entity_copy);
$this->assertEqual($entity_copy, $entity_expected, t('Single object was altered.'));
// Verify alteration of multiple arguments.
$array_copy = $array;
$array_expected = array('foo' => 'Drupal theme');
$entity_copy = clone $entity;
$entity_expected = clone $entity;
$entity_expected->foo = 'Drupal theme';
$array2_copy = $array;
$array2_expected = array('foo' => 'Drupal theme');
drupal_alter('drupal_alter', $array_copy, $entity_copy, $array2_copy);
$this->assertEqual($array_copy, $array_expected, t('First argument to drupal_alter() was altered.'));
$this->assertEqual($entity_copy, $entity_expected, t('Second argument to drupal_alter() was altered.'));
$this->assertEqual($array2_copy, $array2_expected, t('Third argument to drupal_alter() was altered.'));
// Verify alteration order when passing an array of types to drupal_alter().
// common_test_module_implements_alter() places 'block' implementation after
// other modules.
$array_copy = $array;
$array_expected = array('foo' => 'Drupal block theme');
drupal_alter(array('drupal_alter', 'drupal_alter_foo'), $array_copy);
$this->assertEqual($array_copy, $array_expected, t('hook_TYPE_alter() implementations ran in correct order.'));
}
}
<?php
/**
* @file
* Definition of Drupal\system\Tests\Common\ArrayUnitTest.
*/
namespace Drupal\system\Tests\Common;
use Drupal\simpletest\UnitTestBase;
/**
* Tests the various drupal_array_* helper functions.
*/
class ArrayUnitTest extends UnitTestBase {
/**
* Form array to check.
*/
protected $form;
/**
* Array of parents for the nested element.
*/
protected $parents;
public static function getInfo() {
return array(
'name' => 'drupal_array_*() tests',
'description' => 'Tests the various drupal_array_* helper functions.',
'group' => 'System',
);
}
function setUp() {
parent::setUp();
// Create a form structure with a nested element.
$this->form['fieldset']['element'] = array(
'#value' => 'Nested element',
);
// Set up parent array.
$this->parents = array('fieldset', 'element');
}
/**
* Tests getting nested array values.
*/
function testGet() {
// Verify getting a value of a nested element.
$value = drupal_array_get_nested_value($this->form, $this->parents);
$this->assertEqual($value['#value'], 'Nested element', 'Nested element value found.');
// Verify changing a value of a nested element by reference.
$value = &drupal_array_get_nested_value($this->form, $this->parents);
$value['#value'] = 'New value';
$value = drupal_array_get_nested_value($this->form, $this->parents);
$this->assertEqual($value['#value'], 'New value', 'Nested element value was changed by reference.');
$this->assertEqual($this->form['fieldset']['element']['#value'], 'New value', 'Nested element value was changed by reference.');
// Verify that an existing key is reported back.
$key_exists = NULL;
drupal_array_get_nested_value($this->form, $this->parents, $key_exists);
$this->assertIdentical($key_exists, TRUE, 'Existing key found.');
// Verify that a non-existing key is reported back and throws no errors.
$key_exists = NULL;
$parents = $this->parents;
$parents[] = 'foo';
drupal_array_get_nested_value($this->form, $parents, $key_exists);
$this->assertIdentical($key_exists, FALSE, 'Non-existing key not found.');
}
/**
* Tests setting nested array values.
*/
function testSet() {
$new_value = array(
'#value' => 'New value',
'#required' => TRUE,
);
// Verify setting the value of a nested element.
drupal_array_set_nested_value($this->form, $this->parents, $new_value);
$this->assertEqual($this->form['fieldset']['element']['#value'], 'New value', 'Changed nested element value found.');
$this->assertIdentical($this->form['fieldset']['element']['#required'], TRUE, 'New nested element value found.');
}
/**
* Tests unsetting nested array values.
*/
function testUnset() {
// Verify unsetting a non-existing nested element throws no errors and the
// non-existing key is properly reported.
$key_existed = NULL;
$parents = $this->parents;
$parents[] = 'foo';
drupal_array_unset_nested_value($this->form, $parents, $key_existed);
$this->assertTrue(isset($this->form['fieldset']['element']['#value']), 'Outermost nested element key still exists.');
$this->assertIdentical($key_existed, FALSE, 'Non-existing key not found.');
// Verify unsetting a nested element.
$key_existed = NULL;
drupal_array_unset_nested_value($this->form, $this->parents, $key_existed);
$this->assertFalse(isset($this->form['fieldset']['element']), 'Removed nested element not found.');
$this->assertIdentical($key_existed, TRUE, 'Existing key was found.');
}
/**
* Tests existence of array key.
*/
function testKeyExists() {
// Verify that existing key is found.
$this->assertIdentical(drupal_array_nested_key_exists($this->form, $this->parents), TRUE, 'Nested key found.');
// Verify that non-existing keys are not found.
$parents = $this->parents;
$parents[] = 'foo';
$this->assertIdentical(drupal_array_nested_key_exists($this->form, $parents), FALSE, 'Non-existing nested key not found.');
}
}
<?php
/**
* @file
* Definition of Drupal\system\Tests\Common\AttributesUnitTest.
*/
namespace Drupal\system\Tests\Common;
use Drupal\simpletest\UnitTestBase;
/**
* Tests the drupal_attributes() functionality.
*/
class AttributesUnitTest extends UnitTestBase {
public static function getInfo() {
return array(
'name' => 'HTML Attributes',
'description' => 'Tests the drupal_attributes() functionality.',
'group' => 'Common',
);
}
/**
* Tests that drupal_html_class() cleans the class name properly.
*/
function testDrupalAttributes() {
// Verify that special characters are HTML encoded.
$this->assertIdentical(drupal_attributes(array('title' => '&"\'<>')), ' title="&amp;&quot;&#039;&lt;&gt;"', t('HTML encode attribute values.'));
// Verify multi-value attributes are concatenated with spaces.
$attributes = array('class' => array('first', 'last'));
$this->assertIdentical(drupal_attributes(array('class' => array('first', 'last'))), ' class="first last"', t('Concatenate multi-value attributes.'));
// Verify empty attribute values are rendered.
$this->assertIdentical(drupal_attributes(array('alt' => '')), ' alt=""', t('Empty attribute value #1.'));
$this->assertIdentical(drupal_attributes(array('alt' => NULL)), ' alt=""', t('Empty attribute value #2.'));
// Verify multiple attributes are rendered.
$attributes = array(
'id' => 'id-test',
'class' => array('first', 'last'),
'alt' => 'Alternate',
);
$this->assertIdentical(drupal_attributes($attributes), ' id="id-test" class="first last" alt="Alternate"', t('Multiple attributes.'));
// Verify empty attributes array is rendered.
$this->assertIdentical(drupal_attributes(array()), '', t('Empty attributes array.'));
}
}
<?php
/**
* @file
* Definition of Drupal\system\Tests\Common\AutocompleteTagsUnitTest.
*/
namespace Drupal\system\Tests\Common;
use Drupal\simpletest\UnitTestBase;
/**
* Tests drupal_explode_tags() and drupal_implode_tags().
*/
class AutocompleteTagsUnitTest extends UnitTestBase {
var $validTags = array(
'Drupal' => 'Drupal',
'Drupal with some spaces' => 'Drupal with some spaces',
'"Legendary Drupal mascot of doom: ""Druplicon"""' => 'Legendary Drupal mascot of doom: "Druplicon"',
'"Drupal, although it rhymes with sloopal, is as awesome as a troopal!"' => 'Drupal, although it rhymes with sloopal, is as awesome as a troopal!',
);
public static function getInfo() {
return array(
'name' => 'Autocomplete tags',
'description' => 'Tests explosion and implosion of autocomplete tags.',
'group' => 'Common',
);
}
/**
* Explode a series of tags.
*/
function testDrupalExplodeTags() {
$string = implode(', ', array_keys($this->validTags));
$tags = drupal_explode_tags($string);
$this->assertTags($tags);
}
/**
* Implode a series of tags.
*/
function testDrupalImplodeTags() {
$tags = array_values($this->validTags);
// Let's explode and implode to our heart's content.
for ($i = 0; $i < 10; $i++) {
$string = drupal_implode_tags($tags);
$tags = drupal_explode_tags($string);
}
$this->assertTags($tags);
}
/**
* Helper function: asserts that the ending array of tags is what we wanted.
*/
function assertTags($tags) {
$original = $this->validTags;
foreach ($tags as $tag) {
$key = array_search($tag, $original);
$this->assertTrue($key, t('Make sure tag %tag shows up in the final tags array (originally %original)', array('%tag' => $tag, '%original' => $key)));
unset($original[$key]);
}
foreach ($original as $leftover) {
$this->fail(t('Leftover tag %leftover was left over.', array('%leftover' => $leftover)));
}
}
}
<?php
/**
* @file
* Definition of Drupal\system\Tests\Common\CascadingStylesheetsTest.
*/
namespace Drupal\system\Tests\Common;
use Drupal\simpletest\WebTestBase;
/**
* Tests the Drupal CSS system.
*/
class CascadingStylesheetsTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Cascading stylesheets',
'description' => 'Tests adding various cascading stylesheets to the page.',
'group' => 'Common',
);
}
function setUp() {
parent::setUp(array('language', 'common_test'));
// Reset drupal_add_css() before each test.
drupal_static_reset('drupal_add_css');
}
/**
* Check default stylesheets as empty.
*/
function testDefault() {
$this->assertEqual(array(), drupal_add_css(), t('Default CSS is empty.'));
}
/**
* Test that stylesheets in module .info files are loaded.
*/
function testModuleInfo() {
$this->drupalGet('');
// Verify common_test.css in a STYLE media="all" tag.
$elements = $this->xpath('//style[@media=:media and contains(text(), :filename)]', array(
':media' => 'all',
':filename' => 'tests/modules/common_test/common_test.css',
));
$this->assertTrue(count($elements), "Stylesheet with media 'all' in module .info file found.");
// Verify common_test.print.css in a STYLE media="print" tag.
$elements = $this->xpath('//style[@media=:media and contains(text(), :filename)]', array(
':media' => 'print',
':filename' => 'tests/modules/common_test/common_test.print.css',
));
$this->assertTrue(count($elements), "Stylesheet with media 'print' in module .info file found.");
}
/**
* Tests adding a file stylesheet.
*/
function testAddFile() {
$path = drupal_get_path('module', 'simpletest') . '/simpletest.css';
$css = drupal_add_css($path);
$this->assertEqual($css[$path]['data'], $path, t('Adding a CSS file caches it properly.'));
}
/**
* Tests adding an external stylesheet.
*/
function testAddExternal() {
$path = 'http://example.com/style.css';
$css = drupal_add_css($path, 'external');
$this->assertEqual($css[$path]['type'], 'external', t('Adding an external CSS file caches it properly.'));
}
/**
* Makes sure that reseting the CSS empties the cache.
*/
function testReset() {
drupal_static_reset('drupal_add_css');
$this->assertEqual(array(), drupal_add_css(), t('Resetting the CSS empties the cache.'));
}
/**
* Tests rendering the stylesheets.
*/
function testRenderFile() {
$css = drupal_get_path('module', 'simpletest') . '/simpletest.css';
drupal_add_css($css);
$styles = drupal_get_css();
$this->assertTrue(strpos($styles, $css) > 0, t('Rendered CSS includes the added stylesheet.'));
}
/**
* Tests rendering an external stylesheet.
*/
function testRenderExternal() {
$css = 'http://example.com/style.css';
drupal_add_css($css, 'external');
$styles = drupal_get_css();
// Stylesheet URL may be the href of a LINK tag or in an @import statement
// of a STYLE tag.
$this->assertTrue(strpos($styles, 'href="' . $css) > 0 || strpos($styles, '@import url("' . $css . '")') > 0, t('Rendering an external CSS file.'));
}
/**
* Tests rendering inline stylesheets with preprocessing on.
*/
function testRenderInlinePreprocess() {
$css = 'body { padding: 0px; }';
$css_preprocessed = '<style media="all">' . "\n<!--/*--><![CDATA[/*><!--*/\n" . drupal_load_stylesheet_content($css, TRUE) . "\n/*]]>*/-->\n" . '</style>';
drupal_add_css($css, array('type' => 'inline'));
$styles = drupal_get_css();
$this->assertEqual(trim($styles), $css_preprocessed, t('Rendering preprocessed inline CSS adds it to the page.'));
}
/**
* Tests rendering inline stylesheets with preprocessing off.
*/
function testRenderInlineNoPreprocess() {
$css = 'body { padding: 0px; }';
drupal_add_css($css, array('type' => 'inline', 'preprocess' => FALSE));
$styles = drupal_get_css();
$this->assertTrue(strpos($styles, $css) > 0, t('Rendering non-preprocessed inline CSS adds it to the page.'));
}
/**
* Tests rendering inline stylesheets through a full page request.
*/
function testRenderInlineFullPage() {
module_enable(array('php'));
$css = 'body { font-size: 254px; }';
// Inline CSS is minified unless 'preprocess' => FALSE is passed as a
// drupal_add_css() option.
$expected = 'body{font-size:254px;}';
// Create Basic page node type.
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
// Create a node, using the PHP filter that tests drupal_add_css().
$php_format_id = 'php_code';
$settings = array(
'type' => 'page',
'body' => array(
LANGUAGE_NOT_SPECIFIED => array(
array(
'value' => t('This tests the inline CSS!') . "<?php drupal_add_css('$css', 'inline'); ?>",
'format' => $php_format_id,
),
),
),
'promote' => 1,
);
$node = $this->drupalCreateNode($settings);
// Fetch the page.
$this->drupalGet('node/' . $node->nid);
$this->assertRaw($expected, t('Inline stylesheets appear in the full page rendering.'));
}
/**
* Test CSS ordering.
*/
function testRenderOrder() {
// A module CSS file.
drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css');
// A few system CSS files, ordered in a strange way.
$system_path = drupal_get_path('module', 'system');
drupal_add_css($system_path . '/system.base.css', array('group' => CSS_SYSTEM, 'weight' => -10));
drupal_add_css($system_path . '/system.theme.css', array('group' => CSS_SYSTEM));
$expected = array(
$system_path . '/system.base.css',
$system_path . '/system.theme.css',
drupal_get_path('module', 'simpletest') . '/simpletest.css',
);
$styles = drupal_get_css();
// Stylesheet URL may be the href of a LINK tag or in an @import statement
// of a STYLE tag.
if (preg_match_all('/(href="|url\(")' . preg_quote($GLOBALS['base_url'] . '/', '/') . '([^?]+)\?/', $styles, $matches)) {
$result = $matches[2];
}
else {
$result = array();
}
$this->assertIdentical($result, $expected, t('The CSS files are in the expected order.'));
}
/**
* Test CSS override.
*/
function testRenderOverride() {
$system = drupal_get_path('module', 'system');
drupal_add_css($system . '/system.base.css');
drupal_add_css($system . '/tests/system.base.css');
// The dummy stylesheet should be the only one included.
$styles = drupal_get_css();
$this->assert(strpos($styles, $system . '/tests/system.base.css') !== FALSE, t('The overriding CSS file is output.'));
$this->assert(strpos($styles, $system . '/system.base.css') === FALSE, t('The overridden CSS file is not output.'));
drupal_add_css($system . '/tests/system.base.css');
drupal_add_css($system . '/system.base.css');
// The standard stylesheet should be the only one included.
$styles = drupal_get_css();
$this->assert(strpos($styles, $system . '/system.base.css') !== FALSE, t('The overriding CSS file is output.'));
$this->assert(strpos($styles, $system . '/tests/system.base.css') === FALSE, t('The overridden CSS file is not output.'));
}
/**
* Tests Locale module's CSS Alter to include RTL overrides.
*/
function testAlter() {
// Switch the language to a right to left language and add system.base.css.
global $language_interface;
$language_interface->direction = LANGUAGE_RTL;
$path = drupal_get_path('module', 'system');
drupal_add_css($path . '/system.base.css');
// Check to see if system.base-rtl.css was also added.
$styles = drupal_get_css();
$this->assert(strpos($styles, $path . '/system.base-rtl.css') !== FALSE, t('CSS is alterable as right to left overrides are added.'));
// Change the language back to left to right.
$language_interface->direction = LANGUAGE_LTR;
}
/**
* Tests that the query string remains intact when adding CSS files that have
* query string parameters.
*/
function testAddCssFileWithQueryString() {
$this->drupalGet('common-test/query-string');
$query_string = variable_get('css_js_query_string', '0');
$this->assertRaw(drupal_get_path('module', 'node') . '/node.admin.css?' . $query_string, t('Query string was appended correctly to css.'));
$this->assertRaw(drupal_get_path('module', 'node') . '/node-fake.css?arg1=value1&amp;arg2=value2', t('Query string not escaped on a URI.'));
}
}
<?php
/**
* @file
* Definition of Drupal\system\Tests\Common\CascadingStylesheetsUnitTest.
*/
namespace Drupal\system\Tests\Common;
use Drupal\simpletest\UnitTestBase;
/**
* CSS Unit Tests.
*/
class CascadingStylesheetsUnitTest extends UnitTestBase {
public static function getInfo() {
return array(
'name' => 'CSS Unit Tests',
'description' => 'Unit tests on CSS functions like aggregation.',
'group' => 'Common',
);
}
/**
* Tests basic CSS loading with and without optimization via drupal_load_stylesheet().