Commit 7cf7f998 authored by Dries's avatar Dries

- Patch #11218 by David_Rothstein, sun, quicksketch, duncf, awood456,...

- Patch #11218 by David_Rothstein, sun, quicksketch, duncf, awood456, dropcube, mgifford | pwolanin, dww, RobRoy, Crell, webchick, beginner, ray007, bjaspan, chx, Gábor Hojtsy, Steven, Dries, lutegrass, sym, guardian, matt2000, geerlingguy, SeanBannister, matt westgate, com2, praseodym: allow default text formats per role, and integrate text format permissions.
parent 3a2eff71
......@@ -43,6 +43,7 @@ Drupal 7.0, xxxx-xx-xx (development version)
* Redesigned the add content type screen.
* Highlight duplicate URL aliases.
* Renamed "input formats" to "text formats".
* Moved text format permissions to the main permissions page.
* Added configurable ability for users to cancel their own accounts.
* Added "vertical tabs", a reusable interface component that features automatic
summaries and increases usability.
......@@ -80,6 +81,7 @@ Drupal 7.0, xxxx-xx-xx (development version)
fallback to the system time zone and will have to be reconfigured by each user.
- Filter system:
* Revamped the filter API and text format storage.
* Added support for default text formats to be assigned on a per-role basis.
* Refactored the HTML corrector to take advantage of PHP 5 features.
- Removed ping module:
* Contributed modules with similar functionality are available.
......
......@@ -1917,7 +1917,7 @@ function form_process_radios($element) {
* $form['body'] = array(
* '#type' => 'textarea',
* '#title' => t('Body'),
* '#text_format' => isset($node->format) ? $node->format : FILTER_FORMAT_DEFAULT,
* '#text_format' => isset($node->format) ? $node->format : filter_default_format(),
* );
* @endcode
*
......
......@@ -156,7 +156,7 @@ function block_block_info() {
* Implement hook_block_configure().
*/
function block_block_configure($delta = 0) {
$custom_block = array('format' => FILTER_FORMAT_DEFAULT);
$custom_block = array('format' => filter_default_format());
if ($delta) {
$custom_block = block_custom_block_get($delta);
}
......@@ -350,12 +350,12 @@ function block_custom_block_form($edit = array()) {
'#type' => 'textarea',
'#title' => t('Block body'),
'#default_value' => $edit['body'],
'#text_format' => isset($edit['format']) ? $edit['format'] : FILTER_FORMAT_DEFAULT,
'#text_format' => isset($edit['format']) ? $edit['format'] : filter_default_format(),
'#rows' => 15,
'#description' => t('The content of the block as shown to the user.'),
'#required' => TRUE,
'#weight' => -17,
'#access' => filter_access($edit['format']),
'#access' => filter_access(filter_format_load($edit['format'])),
);
return $form;
......@@ -799,9 +799,9 @@ function block_user_role_delete($role) {
/**
* Implement hook_filter_format_delete().
*/
function block_filter_format_delete($format, $default) {
function block_filter_format_delete($format, $fallback) {
db_update('block_custom')
->fields(array('format' => $default->format))
->fields(array('format' => $fallback->format))
->condition('format', $format->format)
->execute();
}
......
......@@ -1814,7 +1814,7 @@ function comment_form($form, &$form_state, $comment) {
'#title' => t('Comment'),
'#rows' => 15,
'#default_value' => $default,
'#text_format' => isset($comment->format) ? $comment->format : FILTER_FORMAT_DEFAULT,
'#text_format' => isset($comment->format) ? $comment->format : filter_default_format(),
'#required' => TRUE,
);
......@@ -2418,9 +2418,9 @@ function comment_menu_alter(&$items) {
/**
* Implement hook_filter_format_delete().
*/
function comment_filter_format_delete($format, $default) {
function comment_filter_format_delete($format, $fallback) {
db_update('comment')
->fields(array('format' => $default->format))
->fields(array('format' => $fallback->format))
->condition('format', $format->format)
->execute();
}
......
......@@ -541,7 +541,7 @@ function text_element_info() {
'#delta' => 0,
'#process' => array('text_textarea_elements_process'),
'#theme_wrappers' => array('text_textarea'),
'#filter_value' => FILTER_FORMAT_DEFAULT,
'#filter_value' => filter_default_format(),
);
$types['text_textarea_with_summary'] = array(
'#input' => TRUE,
......@@ -549,7 +549,7 @@ function text_element_info() {
'#delta' => 0,
'#process' => array('text_textarea_with_summary_process'),
'#theme_wrappers' => array('text_textarea'),
'#filter_value' => FILTER_FORMAT_DEFAULT,
'#filter_value' => filter_default_format(),
);
return $types;
}
......@@ -651,7 +651,7 @@ function text_textfield_elements_process($element, $form_state, $form) {
if (!empty($instance['settings']['text_processing'])) {
$filter_key = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
$format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
$format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : filter_default_format();
$element[$field_key]['#text_format'] = $format;
}
......@@ -684,7 +684,7 @@ function text_textarea_elements_process($element, $form_state, $form) {
if (!empty($instance['settings']['text_processing'])) {
$filter_key = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
$format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
$format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : filter_default_format();
$element[$field_key]['#text_format'] = $format;
}
......@@ -739,7 +739,7 @@ function text_textarea_with_summary_process($element, $form_state, $form) {
if (!empty($instance['settings']['text_processing'])) {
$filter_key = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
$format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
$format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : filter_default_format();
$element[$field_key]['#text_format'] = $format;
}
......@@ -760,7 +760,7 @@ function text_field_widget_formatted_text_value($form, $edit = FALSE) {
// The format selector uses #access = FALSE if only one format is
// available. In this case, we don't receive its value, and need to
// manually set it.
$edit['format'] = !empty($edit[$default_key]) ? $edit[$default_key] : filter_resolve_format(FILTER_FORMAT_DEFAULT);
$edit['format'] = !empty($edit[$default_key]) ? $edit[$default_key] : filter_default_format();
unset($edit[$default_key]);
return $edit;
}
......
......@@ -3,6 +3,8 @@
class TextFieldTestCase extends DrupalWebTestCase {
protected $instance;
protected $admin_user;
protected $web_user;
public static function getInfo() {
return array(
......@@ -15,8 +17,9 @@ class TextFieldTestCase extends DrupalWebTestCase {
function setUp() {
parent::setUp('field_test');
$web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
$this->drupalLogin($web_user);
$this->admin_user = $this->drupalCreateUser(array('administer filters'));
$this->web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
$this->drupalLogin($this->web_user);
}
// Test fields.
......@@ -147,15 +150,23 @@ class TextFieldTestCase extends DrupalWebTestCase {
field_create_instance($this->instance);
$langcode = FIELD_LANGUAGE_NONE;
// Display creation form.
// By default, the user only has access to 'Filtered HTML', and no format
// selector is displayed
// Delete all text formats besides the plain text fallback format.
$this->drupalLogin($this->admin_user);
foreach (filter_formats() as $format) {
if ($format->format != filter_fallback_format()) {
$this->drupalPost('admin/config/content/formats/' . $format->format . '/delete', array(), t('Delete'));
}
}
$this->drupalLogin($this->web_user);
// Display the creation form. Since the user only has access to one format,
// no format selector will be displayed.
$this->drupalGet('test-entity/add/test-bundle');
$this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', t('Widget is displayed'));
$this->assertNoFieldByName("{$this->field_name}[$langcode][0][value_format]", '1', t('Format selector is not displayed'));
$this->assertNoFieldByName("{$this->field_name}[$langcode][0][value_format]", '', t('Format selector is not displayed'));
// Submit with data that should be filtered.
$value = $this->randomName() . '<br />' . $this->randomName();
$value = '<em>' . $this->randomName() . '</em>';
$edit = array(
"{$this->field_name}[$langcode][0][value]" => $value,
);
......@@ -168,21 +179,31 @@ class TextFieldTestCase extends DrupalWebTestCase {
$entity = field_test_entity_load($id);
$entity->content = field_attach_view($entity_type, $entity);
$this->content = drupal_render($entity->content);
$this->assertNoRaw($value, 'Filtered tags are not displayed');
$this->assertRaw(str_replace('<br />', '', $value), t('Filtered value is displayed correctly'));
$this->assertNoRaw($value, t('HTML tags are not displayed.'));
$this->assertRaw(check_plain($value), t('Escaped HTML is displayed correctly.'));
// Allow the user to use the 'Full HTML' format.
db_update('filter_format')->fields(array('roles' => ',2,'))->condition('format', 2)->execute();
// Create a new text format that does not escape HTML, and grant the user
// access to it.
$this->drupalLogin($this->admin_user);
$edit = array('name' => $this->randomName());
$this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
filter_format_reset_cache();
$this->checkPermissions(array(), TRUE);
$format_id = db_query("SELECT format FROM {filter_format} WHERE name = :name", array(':name' => $edit['name']))->fetchField();
$permission = filter_permission_name(filter_format_load($format_id));
$rid = max(array_keys($this->web_user->roles));
user_role_set_permissions($rid, array($permission), TRUE);
$this->drupalLogin($this->web_user);
// Display edition form.
// We should now have a 'text format' selector.
$this->drupalGet('test-entity/' . $id . '/edit');
$this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', t('Widget is displayed'));
$this->assertFieldByName("{$this->field_name}[$langcode][0][value_format]", '1', t('Format selector is displayed'));
$this->assertFieldByName("{$this->field_name}[$langcode][0][value_format]", '', t('Format selector is displayed'));
// Edit and change the format to 'Full HTML'.
// Edit and change the text format to the new one that was created.
$edit = array(
"{$this->field_name}[$langcode][0][value_format]" => 2,
"{$this->field_name}[$langcode][0][value_format]" => $format_id,
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), t('Entity was updated'));
......
......@@ -7,8 +7,7 @@
*/
/**
* Menu callback; Displays a list of all text formats and which
* one is the default.
* Menu callback; Displays a list of all text formats and allows them to be rearranged.
*
* @ingroup forms
* @see filter_admin_overview_submit()
......@@ -16,37 +15,33 @@
function filter_admin_overview($form) {
// Overview of all formats.
$formats = filter_formats();
$error = FALSE;
$fallback_format = filter_fallback_format();
$form['#tree'] = TRUE;
foreach ($formats as $id => $format) {
$roles = array();
foreach (user_roles() as $rid => $name) {
// Prepare a roles array with roles that may access the filter.
if (strpos($format->roles, ",$rid,") !== FALSE) {
$roles[] = $name;
}
// Check whether this is the fallback text format. This format is available
// to all roles and cannot be deleted via the admin interface.
$form['formats'][$id]['#is_fallback'] = ($id == $fallback_format);
if ($form['formats'][$id]['#is_fallback']) {
$form['formats'][$id]['name'] = array('#markup' => theme('placeholder', $format->name));
$roles_markup = theme('placeholder', t('All roles may use this format'));
}
else {
$form['formats'][$id]['name'] = array('#markup' => check_plain($format->name));
$roles = filter_get_roles_by_format($format);
$roles_markup = $roles ? implode(', ', $roles) : t('No roles may use this format');
}
$default = ($id == variable_get('filter_default_format', 1));
$options[$id] = '';
$form[$id]['name'] = array('#markup' => $format->name);
$form[$id]['roles'] = array('#markup' => $default ? t('All roles may use the default format') : ($roles ? implode(', ', $roles) : t('No roles may use this format')));
$form[$id]['configure'] = array('#markup' => l(t('configure'), 'admin/config/content/formats/' . $id));
$form[$id]['delete'] = array('#markup' => $default ? '' : l(t('delete'), 'admin/config/content/formats/delete/' . $id));
$form[$id]['weight'] = array('#type' => 'weight', '#default_value' => $format->weight);
$form['formats'][$id]['roles'] = array('#markup' => $roles_markup);
$form['formats'][$id]['configure'] = array('#markup' => l(t('configure'), 'admin/config/content/formats/' . $id));
$form['formats'][$id]['delete'] = array('#markup' => $form['formats'][$id]['#is_fallback'] ? '' : l(t('delete'), 'admin/config/content/formats/delete/' . $id));
$form['formats'][$id]['weight'] = array('#type' => 'weight', '#default_value' => $format->weight);
}
$form['default'] = array('#type' => 'radios', '#options' => $options, '#default_value' => variable_get('filter_default_format', 1));
$form['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
return $form;
}
function filter_admin_overview_submit($form, &$form_state) {
// Process form submission to set the default format.
if (is_numeric($form_state['values']['default'])) {
drupal_set_message(t('Default format updated.'));
variable_set('filter_default_format', $form_state['values']['default']);
}
foreach ($form_state['values'] as $id => $data) {
foreach ($form_state['values']['formats'] as $id => $data) {
if (is_array($data) && isset($data['weight'])) {
// Only update if this is a form element with weight.
db_update('filter_format')
......@@ -55,35 +50,31 @@ function filter_admin_overview_submit($form, &$form_state) {
->execute();
}
}
filter_formats_reset();
drupal_set_message(t('The text format ordering has been saved.'));
}
/**
* Theme the admin overview form.
* Theme the text format administration overview form.
*
* @ingroup themeable
*/
function theme_filter_admin_overview($form) {
$rows = array();
foreach (element_children($form) as $id) {
$element = $form[$id];
if (isset($element['roles']) && is_array($element['roles'])) {
$element['weight']['#attributes']['class'] = array('text-format-order-weight');
$rows[] = array(
'data' => array(
check_plain($element['name']['#markup']),
drupal_render($element['roles']),
drupal_render($form['default'][$id]),
drupal_render($element['weight']),
drupal_render($element['configure']),
drupal_render($element['delete']),
),
'class' => array('draggable'),
);
unset($form[$id]);
}
foreach (element_children($form['formats']) as $id) {
$form['formats'][$id]['weight']['#attributes']['class'] = array('text-format-order-weight');
$rows[] = array(
'data' => array(
drupal_render($form['formats'][$id]['name']),
drupal_render($form['formats'][$id]['roles']),
drupal_render($form['formats'][$id]['weight']),
drupal_render($form['formats'][$id]['configure']),
drupal_render($form['formats'][$id]['delete']),
),
'class' => array('draggable'),
);
}
$header = array(t('Name'), t('Roles'), t('Default'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
$header = array(t('Name'), t('Roles'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
$output = theme('table', $header, $rows, array('id' => 'text-format-order'));
$output .= drupal_render_children($form);
......@@ -98,7 +89,7 @@ function theme_filter_admin_overview($form) {
function filter_admin_format_page($format = NULL) {
if (!isset($format->name)) {
drupal_set_title(t('Add text format'), PASS_THROUGH);
$format = (object)array('name' => '', 'roles' => '', 'format' => FILTER_FORMAT_DEFAULT);
$format = (object)array('name' => '', 'format' => 0);
}
return drupal_get_form('filter_admin_format_form', $format);
}
......@@ -111,10 +102,9 @@ function filter_admin_format_page($format = NULL) {
* @see filter_admin_format_form_submit()
*/
function filter_admin_format_form($form, &$form_state, $format) {
$default = ($format->format == variable_get('filter_default_format', 1));
if ($default) {
$help = t('All roles for the default format must be enabled and cannot be changed.');
$form['default_format'] = array('#type' => 'hidden', '#value' => 1);
$is_fallback = ($format->format == filter_fallback_format());
if ($is_fallback) {
$help = t('All roles for this text format must be enabled and cannot be changed.');
}
$form['name'] = array(
......@@ -128,17 +118,16 @@ function filter_admin_format_form($form, &$form_state, $format) {
// Add a row of checkboxes for form group.
$form['roles'] = array('#type' => 'fieldset',
'#title' => t('Roles'),
'#description' => $default ? $help : t('Choose which roles may use this text format. Note that roles with the "administer filters" permission can always use all text formats.'),
'#description' => $is_fallback ? $help : t('Choose which roles may use this text format. Note that roles with the "administer filters" permission can always use all text formats.'),
'#tree' => TRUE,
);
$checked = filter_get_roles_by_format($format);
foreach (user_roles() as $rid => $name) {
$checked = strpos($format->roles, ",$rid,") !== FALSE;
$form['roles'][$rid] = array('#type' => 'checkbox',
'#title' => $name,
'#default_value' => ($default || $checked),
'#default_value' => ($is_fallback || isset($checked[$rid])),
);
if ($default) {
if ($is_fallback) {
$form['roles'][$rid]['#disabled'] = TRUE;
}
}
......@@ -201,13 +190,19 @@ function filter_admin_format_form_submit($form, &$form_state) {
$format->format = isset($form_state['values']['format']) ? $form_state['values']['format'] : NULL;
$status = filter_format_save($format);
if ($permission = filter_permission_name($format)) {
foreach ($format->roles as $rid => $enabled) {
user_role_change_permissions($rid, array($permission => $enabled));
}
}
switch ($status) {
case SAVED_NEW:
drupal_set_message(t('Added text format %format.', array('%format' => $format->name)));
break;
case SAVED_UPDATED:
drupal_set_message(t('The text format settings have been updated.'));
drupal_set_message(t('The text format %format has been updated.', array('%format' => $format->name)));
break;
}
}
......@@ -219,26 +214,15 @@ function filter_admin_format_form_submit($form, &$form_state) {
* @see filter_admin_delete_submit()
*/
function filter_admin_delete($form, &$form_state, $format) {
if ($format) {
if ($format->format != variable_get('filter_default_format', 1)) {
$form['#format'] = $format;
return confirm_form($form,
t('Are you sure you want to delete the text format %format?', array('%format' => $format->name)),
'admin/config/content/formats',
t('If you have any content left in this text format, it will be switched to the default text format. This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
else {
drupal_set_message(t('The default format cannot be deleted.'));
drupal_goto('admin/config/content/formats');
}
}
else {
drupal_not_found();
}
$form['#format'] = $format;
return confirm_form($form,
t('Are you sure you want to delete the text format %format?', array('%format' => $format->name)),
'admin/config/content/formats',
t('If you have any content left in this text format, it will be switched to the %fallback text format. This action cannot be undone.', array('%fallback' => filter_fallback_format_title())),
t('Delete'),
t('Cancel')
);
}
/**
......
......@@ -236,20 +236,21 @@ function hook_filter_format_update($format) {
*
* It is recommended for modules to implement this hook, when they store
* references to text formats to replace existing references to the deleted
* text format with the default format.
* text format with the fallback format.
*
* @param $format
* The format object of the format being deleted.
* @param $default
* The format object of the site's default format.
* @param $fallback
* The format object of the site's fallback format, which is always available
* to all users.
*
* @see hook_filter_format_update().
* @see hook_filter_format_delete().
*/
function hook_filter_format_delete($format, $default) {
// Replace the deleted format with the default format.
function hook_filter_format_delete($format, $fallback) {
// Replace the deleted format with the fallback format.
db_update('my_module_table')
->fields(array('format' => $default->format))
->fields(array('format' => $fallback->format))
->condition('format', $format->format)
->execute();
}
......
......@@ -77,13 +77,6 @@ function filter_schema() {
'default' => '',
'description' => 'Name of the text format (Filtered HTML).',
),
'roles' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'A comma-separated string of roles; references {role}.rid.', // This is bad since you can't use joins, nor index.
),
'cache' => array(
'type' => 'int',
'not null' => TRUE,
......@@ -111,6 +104,11 @@ function filter_schema() {
return $schema;
}
/**
* @defgroup updates-6.x-to-7.x Filter updates from 6.x to 7.x
* @{
*/
/**
* Add a weight column to the filter formats table.
*/
......@@ -260,3 +258,88 @@ function filter_update_7004() {
return $ret;
}
/**
* Integrate text formats with the user permissions system.
*
* This function converts text format role assignments to use the new text
* format permissions introduced in Drupal 7, creates a fallback (plain text)
* format that is available to all users, and explicitly sets the text format
* in cases that used to rely on a single site-wide default.
*/
function filter_update_7005() {
$ret = array();
// Move role data from the filter system to the user permission system.
$all_roles = array_keys(user_roles());
$default_format = variable_get('filter_default_format', 1);
$result = db_query("SELECT * FROM {filter_format}");
foreach ($result as $format) {
// We need to assign the default format to all roles (regardless of what
// was stored in the database) to preserve the behavior of the site at the
// moment of the upgrade.
$format_roles = ($format->format == $default_format ? $all_roles : explode(',', $format->roles));
foreach ($format_roles as $format_role) {
if (in_array($format_role, $all_roles)) {
user_role_grant_permissions($format_role, array(filter_permission_name($format)));
}
}
}
// Drop the roles field from the {filter_format} table.
db_drop_field($ret, 'filter_format', 'roles');
// Add a fallback text format which outputs plain text and appears last on
// the list for all users. Generate a unique name for it, starting with
// "Plain text".
$start_name = 'Plain text';
$format_name = $start_name;
while ($format = db_query('SELECT format FROM {filter_format} WHERE name = :name', array(':name' => $format_name))->fetchField()) {
$id = empty($id) ? 1 : $id + 1;
$format_name = $start_name . ' ' . $id;
}
$fallback_format = new stdClass();
$fallback_format->name = $format_name;
$fallback_format->cache = 1;
$fallback_format->weight = 1;
// This format should output plain text, so we escape all HTML and apply the
// line break filter only.
$fallback_format->filters = array(
'filter_html_escape' => array('status' => 1),
'filter_autop' => array('status' => 1),
);
filter_format_save($fallback_format);
variable_set('filter_fallback_format', $fallback_format->format);
drupal_set_message('A new <em>Plain text</em> format has been created which will be available to all users. You can configure this text format on the <a href="' . url('admin/config/content/formats/' . $fallback_format->format) . '">text format configuration page</a>.');
// Move the former site-wide default text format to the top of the list, so
// that it continues to be the default text format for all users.
db_update('filter_format')
->fields(array('weight' => -1))
->condition('format', $default_format)
->execute();
// It was previously possible for a value of "0" to be stored in database
// tables to indicate that a particular piece of text should be filtered
// using the default text format. Therefore, we have to convert all such
// instances (in Drupal core) to explicitly use the appropriate format.
// Note that the update of the node body field is handled separately, in
// node_update_7006().
foreach (array('block_custom', 'comment') as $table) {
if (db_table_exists($table)) {
db_update($table)
->fields(array('format' => $default_format))
->condition('format', 0)
->execute();
}
}
// We do not delete the 'filter_default_format' variable, since other modules
// may need it in their update functions.
// @todo This variable can be deleted in Drupal 8.
return $ret;
}
/**
* @} End of "defgroup updates-6.x-to-7.x"
* The next series of updates should start at 8000.
*/
This diff is collapsed.
This diff is collapsed.
......@@ -506,7 +506,11 @@ function node_update_7006(&$context) {
$revision->body = substr($revision->body, strlen($break));
}
$node->body[FIELD_LANGUAGE_NONE][0]['value'] = $revision->body;
$node->body[FIELD_LANGUAGE_NONE][0]['format'] = $revision->format;
// Explicitly store the current default text format if the revision
// did not have its own text format. Similar conversions for other
// core modules are performed in filter_update_7005(), but we do this
// one here since we are already migrating the data.
$node->body[FIELD_LANGUAGE_NONE][0]['format'] = !empty($revision->format) ? $revision->format : variable_get('filter_default_format', 1);
// This is a core update and no contrib modules are enabled yet, so
// we can assume default field storage for a faster update.
field_sql_storage_field_storage_write('node', $node, FIELD_STORAGE_INSERT, array());
......
......@@ -19,7 +19,6 @@ function php_install() {
$format = db_insert('filter_format')
->fields(array(
'name' => 'PHP code',
'roles' => '',
'cache' => 0,
))
->execute();
......
......@@ -5,6 +5,8 @@
* Base PHP test case class.
*/
class PHPTestCase extends DrupalWebTestCase {
protected $php_code_format;
function setUp() {
parent::setUp('php');
......@@ -12,9 +14,14 @@ class PHPTestCase extends DrupalWebTestCase {
$admin_user = $this->drupalCreateUser(array('administer filters'));
$this->drupalLogin($admin_user);
// Confirm that the PHP filter is #3.
$this->drupalGet('admin/config/content/formats/3');
$this->assertText('PHP code', t('On PHP code filter page.'));
// Confirm that the PHP code text format was inserted as the newest format
// on the site.
$newest_format_id = db_query("SELECT MAX(format) FROM {filter_format}")->fetchField();
$newest_format = filter_format_load($newest_format_id);
$this->assertEqual($newest_format->name, 'PHP code', t('PHP code text format was created.'));
// Store the format ID of the PHP code text format for later use.
$this->php_code_format = $newest_format_id;
}
/**
......@@ -43,15 +50,12 @@ class PHPFilterTestCase extends PHPTestCase {
* Make sure that the PHP filter evaluates PHP code when used.
*/
function testPHPFilter() {
// Setup PHP filter.
$edit = array();
$edit['roles[2]'] = TRUE; // Set authenticated users to have permission to use filter.
$this->drupalPost(NULL, $edit, 'Save configuration');
$this->assertText(t('The text format settings have been updated.'), t('PHP format available to authenticated users.'));
// Create node with PHP filter enabled.
$web_user = $this->drupalCreateUser(array('access content', 'create page content', 'edit own page content'));
// Log in as a user with permission to use the PHP code text format.
$php_code_permission = filter_permission_name(filter_format_load($this->php_code_format));
$web_user = $this->drupalCreateUser(array('access content', 'create page content', 'edit own page content', $php_code_permission));
$this->drupalLogin($web_user);
// Create a node with PHP code in it.
$node = $this->createNodeWithCode();
// Make sure that the PHP code shows up as text.
......@@ -61,7 +65,7 @@ class PHPFilterTestCase extends PHPTestCase {
// Change filter to PHP filter and see that PHP code is evaluated.
$edit = array();
$langcode = FIELD_LANGUAGE_NONE;
$edit["body[$langcode][0][value_format]"] = 3;
$edit["body[$langcode][0][value_format]"] = $this->php_code_format;
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), t('PHP code filter turned on.'));
......@@ -98,6 +102,6 @@ class PHPAccessTestCase extends PHPTestCase {
// Make sure that user doesn't have access to filter.
$this->drupalGet('node/' . $node->nid . '/edit');
$this->assertNoFieldByName('body_format', '3', t('Format not available.'));
$this->assertNoRaw('<option value="' . $this->php_code_format . '">', t('PHP code format not available.'));
}
}
......@@ -292,7 +292,7 @@ function profile_view_field($account, $field) {
if (isset($account->{$field->name}) && $value = $account->{$field->name}) {
switch ($field->type) {
case 'textarea':
return check_markup($value);
return check_markup($value, filter_default_format($account));
case 'textfield':
case 'selection':
return $browse ? l($value, 'profile/' . $field->name . '/' . $value) : check_plain($value);
......
......@@ -728,7 +728,7 @@ protected function drupalCreateNode($settings = array()) {
// Merge body field value and format separately.
$body = array(
'value' => $this->randomName(32),
'format' => FILTER_FORMAT_DEFAULT
'format' => filter_default_format(),
);
$settings['body'][FIELD_LANGUAGE_NONE][0] += $body;
......
......@@ -374,7 +374,17 @@ class CascadingStylesheetsTestCase extends DrupalWebTestCase {
// Create a node, using the PHP filter that tests drupal_add_css().
$settings = array(
'type' => 'page',
'body' => array(FIELD_LANGUAGE_NONE => array(array('value' => t('This tests the inline CSS!') . "<?php drupal_add_css('$css', 'inline'); ?>", 'format' => 3))), // PHP filter.
'body' => array(
FIELD_LANGUAGE_NONE => array(
array(
'value' => t('This tests the inline CSS!') . "<?php drupal_add_css('$css', 'inline'); ?>",
// The "PHP code" format is always the most recent one added, since
// the PHP module was enabled in the setUp() method of the current
// test.
'format' => db_query("SELECT MAX(format) FROM {filter_format}")->fetchField(),
),
),
),
'promote' => 1,
);