Commit 8b9b034c authored by catch's avatar catch

Issue #1591436 by aspilicious, Rob Loach: Convert filter tests to PSR-0.

parent d3df59e5
......@@ -3,6 +3,5 @@ description = Filters content in preparation for display.
package = Core
version = VERSION
core = 8.x
files[] = filter.test
required = TRUE
configure = admin/config/content/formats
<?php
/**
* @file
* Definition of Drupal\filter\Tests\FilterCrudTest.
*/
namespace Drupal\filter\Tests;
use Drupal\simpletest\WebTestBase;
use stdClass;
/**
* Tests for text format and filter CRUD operations.
*/
class FilterCrudTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Filter CRUD operations',
'description' => 'Test creation, loading, updating, deleting of text formats and filters.',
'group' => 'Filter',
);
}
function setUp() {
parent::setUp('filter_test');
}
/**
* Test CRUD operations for text formats and filters.
*/
function testTextFormatCrud() {
// Add a text format with minimum data only.
$format = new stdClass();
$format->format = 'empty_format';
$format->name = 'Empty format';
filter_format_save($format);
$this->verifyTextFormat($format);
$this->verifyFilters($format);
// Add another text format specifying all possible properties.
$format = new stdClass();
$format->format = 'custom_format';
$format->name = 'Custom format';
$format->filters = array(
'filter_url' => array(
'status' => 1,
'settings' => array(
'filter_url_length' => 30,
),
),
);
filter_format_save($format);
$this->verifyTextFormat($format);
$this->verifyFilters($format);
// Alter some text format properties and save again.
$format->name = 'Altered format';
$format->filters['filter_url']['status'] = 0;
$format->filters['filter_autop']['status'] = 1;
filter_format_save($format);
$this->verifyTextFormat($format);
$this->verifyFilters($format);
// Add a uncacheable filter and save again.
$format->filters['filter_test_uncacheable']['status'] = 1;
filter_format_save($format);
$this->verifyTextFormat($format);
$this->verifyFilters($format);
// Disable the text format.
filter_format_disable($format);
$db_format = db_query("SELECT * FROM {filter_format} WHERE format = :format", array(':format' => $format->format))->fetchObject();
$this->assertFalse($db_format->status, t('Database: Disabled text format is marked as disabled.'));
$formats = filter_formats();
$this->assertTrue(!isset($formats[$format->format]), t('filter_formats: Disabled text format no longer exists.'));
}
/**
* Verify that a text format is properly stored.
*/
function verifyTextFormat($format) {
$t_args = array('%format' => $format->name);
// Verify text format database record.
$db_format = db_select('filter_format', 'ff')
->fields('ff')
->condition('format', $format->format)
->execute()
->fetchObject();
$this->assertEqual($db_format->format, $format->format, t('Database: Proper format id for text format %format.', $t_args));
$this->assertEqual($db_format->name, $format->name, t('Database: Proper title for text format %format.', $t_args));
$this->assertEqual($db_format->cache, $format->cache, t('Database: Proper cache indicator for text format %format.', $t_args));
$this->assertEqual($db_format->weight, $format->weight, t('Database: Proper weight for text format %format.', $t_args));
// Verify filter_format_load().
$filter_format = filter_format_load($format->format);
$this->assertEqual($filter_format->format, $format->format, t('filter_format_load: Proper format id for text format %format.', $t_args));
$this->assertEqual($filter_format->name, $format->name, t('filter_format_load: Proper title for text format %format.', $t_args));
$this->assertEqual($filter_format->cache, $format->cache, t('filter_format_load: Proper cache indicator for text format %format.', $t_args));
$this->assertEqual($filter_format->weight, $format->weight, t('filter_format_load: Proper weight for text format %format.', $t_args));
// Verify the 'cache' text format property according to enabled filters.
$filter_info = filter_get_filters();
$filters = filter_list_format($filter_format->format);
$cacheable = TRUE;
foreach ($filters as $name => $filter) {
// If this filter is not cacheable, update $cacheable accordingly, so we
// can verify $format->cache after iterating over all filters.
if ($filter->status && isset($filter_info[$name]['cache']) && !$filter_info[$name]['cache']) {
$cacheable = FALSE;
break;
}
}
$this->assertEqual($filter_format->cache, $cacheable, t('Text format contains proper cache property.'));
}
/**
* Verify that filters are properly stored for a text format.
*/
function verifyFilters($format) {
// Verify filter database records.
$filters = db_query("SELECT * FROM {filter} WHERE format = :format", array(':format' => $format->format))->fetchAllAssoc('name');
$format_filters = $format->filters;
foreach ($filters as $name => $filter) {
$t_args = array('%format' => $format->name, '%filter' => $name);
// Verify that filter status is properly stored.
$this->assertEqual($filter->status, $format_filters[$name]['status'], t('Database: Proper status for %filter in text format %format.', $t_args));
// Verify that filter settings were properly stored.
$this->assertEqual(unserialize($filter->settings), isset($format_filters[$name]['settings']) ? $format_filters[$name]['settings'] : array(), t('Database: Proper filter settings for %filter in text format %format.', $t_args));
// Verify that each filter has a module name assigned.
$this->assertTrue(!empty($filter->module), t('Database: Proper module name for %filter in text format %format.', $t_args));
// Remove the filter from the copy of saved $format to check whether all
// filters have been processed later.
unset($format_filters[$name]);
}
// Verify that all filters have been processed.
$this->assertTrue(empty($format_filters), t('Database contains values for all filters in the saved format.'));
// Verify filter_list_format().
$filters = filter_list_format($format->format);
$format_filters = $format->filters;
foreach ($filters as $name => $filter) {
$t_args = array('%format' => $format->name, '%filter' => $name);
// Verify that filter status is properly stored.
$this->assertEqual($filter->status, $format_filters[$name]['status'], t('filter_list_format: Proper status for %filter in text format %format.', $t_args));
// Verify that filter settings were properly stored.
$this->assertEqual($filter->settings, isset($format_filters[$name]['settings']) ? $format_filters[$name]['settings'] : array(), t('filter_list_format: Proper filter settings for %filter in text format %format.', $t_args));
// Verify that each filter has a module name assigned.
$this->assertTrue(!empty($filter->module), t('filter_list_format: Proper module name for %filter in text format %format.', $t_args));
// Remove the filter from the copy of saved $format to check whether all
// filters have been processed later.
unset($format_filters[$name]);
}
// Verify that all filters have been processed.
$this->assertTrue(empty($format_filters), t('filter_list_format: Loaded filters contain values for all filters in the saved format.'));
}
}
<?php
/**
* @file
* Definition of Drupal\filter\Tests\FilterDefaultFormatTest.
*/
namespace Drupal\filter\Tests;
use Drupal\simpletest\WebTestBase;
class FilterDefaultFormatTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Default text format functionality',
'description' => 'Test the default text formats for different users.',
'group' => 'Filter',
);
}
function testDefaultTextFormats() {
// Create two text formats, and two users. The first user has access to
// both formats, but the second user only has access to the second one.
$admin_user = $this->drupalCreateUser(array('administer filters'));
$this->drupalLogin($admin_user);
$formats = array();
for ($i = 0; $i < 2; $i++) {
$edit = array(
'format' => drupal_strtolower($this->randomName()),
'name' => $this->randomName(),
);
$this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
$this->resetFilterCaches();
$formats[] = filter_format_load($edit['format']);
}
list($first_format, $second_format) = $formats;
$first_user = $this->drupalCreateUser(array(filter_permission_name($first_format), filter_permission_name($second_format)));
$second_user = $this->drupalCreateUser(array(filter_permission_name($second_format)));
// Adjust the weights so that the first and second formats (in that order)
// are the two lowest weighted formats available to any user.
$minimum_weight = db_query("SELECT MIN(weight) FROM {filter_format}")->fetchField();
$edit = array();
$edit['formats[' . $first_format->format . '][weight]'] = $minimum_weight - 2;
$edit['formats[' . $second_format->format . '][weight]'] = $minimum_weight - 1;
$this->drupalPost('admin/config/content/formats', $edit, t('Save changes'));
$this->resetFilterCaches();
// Check that each user's default format is the lowest weighted format that
// the user has access to.
$this->assertEqual(filter_default_format($first_user), $first_format->format, t("The first user's default format is the lowest weighted format that the user has access to."));
$this->assertEqual(filter_default_format($second_user), $second_format->format, t("The second user's default format is the lowest weighted format that the user has access to, and is different than the first user's."));
// Reorder the two formats, and check that both users now have the same
// default.
$edit = array();
$edit['formats[' . $second_format->format . '][weight]'] = $minimum_weight - 3;
$this->drupalPost('admin/config/content/formats', $edit, t('Save changes'));
$this->resetFilterCaches();
$this->assertEqual(filter_default_format($first_user), filter_default_format($second_user), t('After the formats are reordered, both users have the same default format.'));
}
/**
* Rebuild text format and permission caches in the thread running the tests.
*/
protected function resetFilterCaches() {
filter_formats_reset();
$this->checkPermissions(array(), TRUE);
}
}
<?php
/**
* @file
* Definition of Drupal\filter\Tests\FilterHooksTest.
*/
namespace Drupal\filter\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests for filter hook invocation.
*/
class FilterHooksTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Filter format hooks',
'description' => 'Test hooks for text formats insert/update/disable.',
'group' => 'Filter',
);
}
function setUp() {
parent::setUp('block', 'filter_test');
$admin_user = $this->drupalCreateUser(array('administer filters', 'administer blocks'));
$this->drupalLogin($admin_user);
}
/**
* Test that hooks run correctly on creating, editing, and deleting a text format.
*/
function testFilterHooks() {
// Add a text format.
$name = $this->randomName();
$edit = array();
$edit['format'] = drupal_strtolower($this->randomName());
$edit['name'] = $name;
$edit['roles[' . DRUPAL_ANONYMOUS_RID . ']'] = 1;
$this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
$this->assertRaw(t('Added text format %format.', array('%format' => $name)), t('New format created.'));
$this->assertText('hook_filter_format_insert invoked.', t('hook_filter_format_insert was invoked.'));
$format_id = $edit['format'];
// Update text format.
$edit = array();
$edit['roles[' . DRUPAL_AUTHENTICATED_RID . ']'] = 1;
$this->drupalPost('admin/config/content/formats/' . $format_id, $edit, t('Save configuration'));
$this->assertRaw(t('The text format %format has been updated.', array('%format' => $name)), t('Format successfully updated.'));
$this->assertText('hook_filter_format_update invoked.', t('hook_filter_format_update() was invoked.'));
// Add a new custom block.
$custom_block = array();
$custom_block['info'] = $this->randomName(8);
$custom_block['title'] = $this->randomName(8);
$custom_block['body[value]'] = $this->randomName(32);
// Use the format created.
$custom_block['body[format]'] = $format_id;
$this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
$this->assertText(t('The block has been created.'), t('New block successfully created.'));
// Verify the new block is in the database.
$bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(':info' => $custom_block['info']))->fetchField();
$this->assertNotNull($bid, t('New block found in database'));
// Disable the text format.
$this->drupalPost('admin/config/content/formats/' . $format_id . '/disable', array(), t('Disable'));
$this->assertRaw(t('Disabled text format %format.', array('%format' => $name)), t('Format successfully disabled.'));
$this->assertText('hook_filter_format_disable invoked.', t('hook_filter_format_disable() was invoked.'));
}
}
<?php
/**
* @file
* Definition of Drupal\filter\Tests\FilterNoFormatTest.
*/
namespace Drupal\filter\Tests;
use Drupal\simpletest\WebTestBase;
class FilterNoFormatTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Unassigned text format functionality',
'description' => 'Test the behavior of check_markup() when it is called without a text format.',
'group' => 'Filter',
);
}
function testCheckMarkupNoFormat() {
// Create some text. Include some HTML and line breaks, so we get a good
// test of the filtering that is applied to it.
$text = "<strong>" . $this->randomName(32) . "</strong>\n\n<div>" . $this->randomName(32) . "</div>";
// Make sure that when this text is run through check_markup() with no text
// format, it is filtered as though it is in the fallback format.
$this->assertEqual(check_markup($text), check_markup($text, filter_fallback_format()), t('Text with no format is filtered the same as text in the fallback format.'));
}
}
<?php
/**
* @file
* Definition of Drupal\filter\Tests\FilterSecurityTest.
*/
namespace Drupal\filter\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Security tests for missing/vanished text formats or filters.
*/
class FilterSecurityTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Security',
'description' => 'Test the behavior of check_markup() when a filter or text format vanishes.',
'group' => 'Filter',
);
}
function setUp() {
parent::setUp(array('node', 'php', 'filter_test'));
// Create Basic page node type.
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
// Create Filtered HTML format.
$filtered_html_format = array(
'format' => 'filtered_html',
'name' => 'Filtered HTML',
);
$filtered_html_format = (object) $filtered_html_format;
filter_format_save($filtered_html_format);
$filtered_html_permission = filter_permission_name($filtered_html_format);
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array($filtered_html_permission));
$this->admin_user = $this->drupalCreateUser(array('administer modules', 'administer filters', 'administer site configuration'));
$this->drupalLogin($this->admin_user);
}
/**
* Test that filtered content is emptied when an actively used filter module is disabled.
*/
function testDisableFilterModule() {
// Create a new node.
$node = $this->drupalCreateNode(array('promote' => 1));
$body_raw = $node->body[LANGUAGE_NOT_SPECIFIED][0]['value'];
$format_id = $node->body[LANGUAGE_NOT_SPECIFIED][0]['format'];
$this->drupalGet('node/' . $node->nid);
$this->assertText($body_raw, t('Node body found.'));
// Enable the filter_test_replace filter.
$edit = array(
'filters[filter_test_replace][status]' => 1,
);
$this->drupalPost('admin/config/content/formats/' . $format_id, $edit, t('Save configuration'));
// Verify that filter_test_replace filter replaced the content.
$this->drupalGet('node/' . $node->nid);
$this->assertNoText($body_raw, t('Node body not found.'));
$this->assertText('Filter: Testing filter', t('Testing filter output found.'));
// Disable the text format entirely.
$this->drupalPost('admin/config/content/formats/' . $format_id . '/disable', array(), t('Disable'));
// Verify that the content is empty, because the text format does not exist.
$this->drupalGet('node/' . $node->nid);
$this->assertNoText($body_raw, t('Node body not found.'));
}
}
<?php
/**
* @file
* Definition of Drupal\filter\Tests\FilterSettingsTest.
*/
namespace Drupal\filter\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests filter settings.
*/
class FilterSettingsTest extends WebTestBase {
protected $profile = 'testing';
public static function getInfo() {
return array(
'name' => 'Filter settings',
'description' => 'Tests filter settings.',
'group' => 'Filter',
);
}
/**
* Tests explicit and implicit default settings for filters.
*/
function testFilterDefaults() {
$filter_info = filter_filter_info();
$filters = array_fill_keys(array_keys($filter_info), array());
// Create text format using filter default settings.
$filter_defaults_format = (object) array(
'format' => 'filter_defaults',
'name' => 'Filter defaults',
'filters' => $filters,
);
filter_format_save($filter_defaults_format);
// Verify that default weights defined in hook_filter_info() were applied.
$saved_settings = array();
foreach ($filter_defaults_format->filters as $name => $settings) {
$expected_weight = (isset($filter_info[$name]['weight']) ? $filter_info[$name]['weight'] : 0);
$this->assertEqual($settings['weight'], $expected_weight, format_string('@name filter weight %saved equals %default', array(
'@name' => $name,
'%saved' => $settings['weight'],
'%default' => $expected_weight,
)));
$saved_settings[$name]['weight'] = $expected_weight;
}
// Re-save the text format.
filter_format_save($filter_defaults_format);
// Reload it from scratch.
filter_formats_reset();
$filter_defaults_format = filter_format_load($filter_defaults_format->format);
$filter_defaults_format->filters = filter_list_format($filter_defaults_format->format);
// Verify that saved filter settings have not been changed.
foreach ($filter_defaults_format->filters as $name => $settings) {
$this->assertEqual($settings->weight, $saved_settings[$name]['weight'], format_string('@name filter weight %saved equals %previous', array(
'@name' => $name,
'%saved' => $settings->weight,
'%previous' => $saved_settings[$name]['weight'],
)));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment