Пример #1
0
 /**
  * Retrieves a list of filters for a given text format.
  *
  * @param string $format_name
  *    Text format machine name.
  * @param bool $reset
  *    TRUE to reset filter formats cache.
  *
  * @return array
  *    An array of filter objects associated to the given text format.
  */
 public function getFormatFilters($format_name, $reset = FALSE)
 {
     if ($reset) {
         filter_formats_reset();
     }
     return filter_list_format($format_name);
 }
Пример #2
0
 /**
  * Tests explicit and implicit default settings for filters.
  */
 function testFilterDefaults()
 {
     $filter_info = $this->container->get('plugin.manager.filter')->getDefinitions();
     // Create text format using filter default settings.
     $filter_defaults_format = entity_create('filter_format', array('format' => 'filter_defaults', 'name' => 'Filter defaults'));
     $filter_defaults_format->save();
     // Verify that default weights defined in hook_filter_info() were applied.
     $saved_settings = array();
     foreach ($filter_defaults_format->filters() as $name => $filter) {
         $expected_weight = $filter_info[$name]['weight'];
         $this->assertEqual($filter->weight, $expected_weight, format_string('@name filter weight %saved equals %default', array('@name' => $name, '%saved' => $filter->weight, '%default' => $expected_weight)));
         $saved_settings[$name]['weight'] = $expected_weight;
     }
     // Re-save the text format.
     $filter_defaults_format->save();
     // Reload it from scratch.
     filter_formats_reset();
     // Verify that saved filter settings have not been changed.
     foreach ($filter_defaults_format->filters() as $name => $filter) {
         $this->assertEqual($filter->weight, $saved_settings[$name]['weight'], format_string('@name filter weight %saved equals %previous', array('@name' => $name, '%saved' => $filter->weight, '%previous' => $saved_settings[$name]['weight'])));
     }
 }
Пример #3
0
 /**
  * Helper function for testTextfieldWidgetsFormatted().
  */
 function _testTextfieldWidgetsFormatted($field_type, $widget_type)
 {
     /** @var \Drupal\Core\Render\RendererInterface $renderer */
     $renderer = $this->container->get('renderer');
     // Create a field.
     $field_name = Unicode::strtolower($this->randomMachineName());
     $field_storage = entity_create('field_storage_config', array('field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => $field_type));
     $field_storage->save();
     entity_create('field_config', array('field_storage' => $field_storage, 'bundle' => 'entity_test', 'label' => $this->randomMachineName() . '_label'))->save();
     entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($field_name, array('type' => $widget_type))->save();
     entity_get_display('entity_test', 'entity_test', 'full')->setComponent($field_name)->save();
     // Disable all text formats besides the plain text fallback format.
     $this->drupalLogin($this->adminUser);
     foreach (filter_formats() as $format) {
         if (!$format->isFallbackFormat()) {
             $this->drupalPostForm('admin/config/content/formats/manage/' . $format->id() . '/disable', array(), t('Disable'));
         }
     }
     $this->drupalLogin($this->webUser);
     // Display the creation form. Since the user only has access to one format,
     // no format selector will be displayed.
     $this->drupalGet('entity_test/add');
     $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed');
     $this->assertNoFieldByName("{$field_name}[0][format]", '', 'Format selector is not displayed');
     // Submit with data that should be filtered.
     $value = '<em>' . $this->randomMachineName() . '</em>';
     $edit = array("{$field_name}[0][value]" => $value);
     $this->drupalPostForm(NULL, $edit, t('Save'));
     preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
     $id = $match[1];
     $this->assertText(t('entity_test @id has been created.', array('@id' => $id)), 'Entity was created');
     // Display the entity.
     $entity = entity_load('entity_test', $id);
     $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
     $content = $display->build($entity);
     $this->setRawContent($renderer->renderRoot($content));
     $this->assertNoRaw($value, 'HTML tags are not displayed.');
     $this->assertEscaped($value, 'Escaped HTML is displayed correctly.');
     // Create a new text format that does not escape HTML, and grant the user
     // access to it.
     $this->drupalLogin($this->adminUser);
     $edit = array('format' => Unicode::strtolower($this->randomMachineName()), 'name' => $this->randomMachineName());
     $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
     filter_formats_reset();
     $format = entity_load('filter_format', $edit['format']);
     $format_id = $format->id();
     $permission = $format->getPermissionName();
     $roles = $this->webUser->getRoles();
     $rid = $roles[0];
     user_role_grant_permissions($rid, array($permission));
     $this->drupalLogin($this->webUser);
     // Display edition form.
     // We should now have a 'text format' selector.
     $this->drupalGet('entity_test/manage/' . $id . '/edit');
     $this->assertFieldByName("{$field_name}[0][value]", NULL, 'Widget is displayed');
     $this->assertFieldByName("{$field_name}[0][format]", NULL, 'Format selector is displayed');
     // Edit and change the text format to the new one that was created.
     $edit = array("{$field_name}[0][format]" => $format_id);
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertText(t('entity_test @id has been updated.', array('@id' => $id)), 'Entity was updated');
     // Display the entity.
     $this->container->get('entity.manager')->getStorage('entity_test')->resetCache(array($id));
     $entity = entity_load('entity_test', $id);
     $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
     $content = $display->build($entity);
     $this->setRawContent($renderer->renderRoot($content));
     $this->assertRaw($value, 'Value is displayed unfiltered');
 }
Пример #4
0
 /**
  * {@inheritdoc}
  */
 public function postSave(EntityStorageInterface $storage, $update = TRUE)
 {
     parent::postSave($storage, $update);
     // Clear the static caches of filter_formats() and others.
     filter_formats_reset();
     if (!$update && !$this->isSyncing()) {
         // Default configuration of modules and installation profiles is allowed
         // to specify a list of user roles to grant access to for the new format;
         // apply the defined user role permissions when a new format is inserted
         // and has a non-empty $roles property.
         // Note: user_role_change_permissions() triggers a call chain back into
         // \Drupal\filter\FilterPermissions::permissions() and lastly
         // filter_formats(), so its cache must be reset upfront.
         if (($roles = $this->get('roles')) && ($permission = $this->getPermissionName())) {
             foreach (user_roles() as $rid => $name) {
                 $enabled = in_array($rid, $roles, TRUE);
                 user_role_change_permissions($rid, array($permission => $enabled));
             }
         }
     }
 }
Пример #5
0
 /**
  * Rebuilds text format and permission caches in the thread running the tests.
  */
 protected function resetFilterCaches()
 {
     filter_formats_reset();
 }
Пример #6
0
 /**
  * Tests filter administration functionality.
  */
 function testFilterAdmin()
 {
     $first_filter = 'filter_autop';
     $second_filter = 'filter_url';
     $basic = 'basic_html';
     $restricted = 'restricted_html';
     $full = 'full_html';
     $plain = 'plain_text';
     // Check that the fallback format exists and cannot be disabled.
     $this->assertTrue($plain == filter_fallback_format(), 'The fallback format is set to plain text.');
     $this->drupalGet('admin/config/content/formats');
     $this->assertNoRaw('admin/config/content/formats/manage/' . $plain . '/disable', 'Disable link for the fallback format not found.');
     $this->drupalGet('admin/config/content/formats/manage/' . $plain . '/disable');
     $this->assertResponse(403, 'The fallback format cannot be disabled.');
     // Verify access permissions to Full HTML format.
     $full_format = entity_load('filter_format', $full);
     $this->assertTrue($full_format->access('use', $this->adminUser), 'Admin user may use Full HTML.');
     $this->assertFalse($full_format->access('use', $this->webUser), 'Web user may not use Full HTML.');
     // Add an additional tag.
     $edit = array();
     $edit['filters[filter_html][settings][allowed_html]'] = '<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <quote>';
     $this->drupalPostForm('admin/config/content/formats/manage/' . $restricted, $edit, t('Save configuration'));
     $this->assertUrl('admin/config/content/formats');
     $this->drupalGet('admin/config/content/formats/manage/' . $restricted);
     $this->assertFieldByName('filters[filter_html][settings][allowed_html]', $edit['filters[filter_html][settings][allowed_html]'], 'Allowed HTML tag added.');
     $elements = $this->xpath('//select[@name=:first]/following::select[@name=:second]', array(':first' => 'filters[' . $first_filter . '][weight]', ':second' => 'filters[' . $second_filter . '][weight]'));
     $this->assertTrue(!empty($elements), 'Order confirmed in admin interface.');
     // Reorder filters.
     $edit = array();
     $edit['filters[' . $second_filter . '][weight]'] = 1;
     $edit['filters[' . $first_filter . '][weight]'] = 2;
     $this->drupalPostForm(NULL, $edit, t('Save configuration'));
     $this->assertUrl('admin/config/content/formats');
     $this->drupalGet('admin/config/content/formats/manage/' . $restricted);
     $this->assertFieldByName('filters[' . $second_filter . '][weight]', 1, 'Order saved successfully.');
     $this->assertFieldByName('filters[' . $first_filter . '][weight]', 2, 'Order saved successfully.');
     $elements = $this->xpath('//select[@name=:first]/following::select[@name=:second]', array(':first' => 'filters[' . $second_filter . '][weight]', ':second' => 'filters[' . $first_filter . '][weight]'));
     $this->assertTrue(!empty($elements), 'Reorder confirmed in admin interface.');
     $filter_format = entity_load('filter_format', $restricted);
     foreach ($filter_format->filters() as $filter_name => $filter) {
         if ($filter_name == $second_filter || $filter_name == $first_filter) {
             $filters[] = $filter_name;
         }
     }
     // Ensure that the second filter is now before the first filter.
     $this->assertEqual($filter_format->filters($second_filter)->weight + 1, $filter_format->filters($first_filter)->weight, 'Order confirmed in configuration.');
     // Add format.
     $edit = array();
     $edit['format'] = Unicode::strtolower($this->randomMachineName());
     $edit['name'] = $this->randomMachineName();
     $edit['roles[' . RoleInterface::AUTHENTICATED_ID . ']'] = 1;
     $edit['filters[' . $second_filter . '][status]'] = TRUE;
     $edit['filters[' . $first_filter . '][status]'] = TRUE;
     $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
     $this->assertUrl('admin/config/content/formats');
     $this->assertRaw(t('Added text format %format.', array('%format' => $edit['name'])), 'New filter created.');
     filter_formats_reset();
     $format = entity_load('filter_format', $edit['format']);
     $this->assertNotNull($format, 'Format found in database.');
     $this->drupalGet('admin/config/content/formats/manage/' . $format->id());
     $this->assertFieldByName('roles[' . RoleInterface::AUTHENTICATED_ID . ']', '', 'Role found.');
     $this->assertFieldByName('filters[' . $second_filter . '][status]', '', 'Line break filter found.');
     $this->assertFieldByName('filters[' . $first_filter . '][status]', '', 'Url filter found.');
     // Disable new filter.
     $this->drupalPostForm('admin/config/content/formats/manage/' . $format->id() . '/disable', array(), t('Disable'));
     $this->assertUrl('admin/config/content/formats');
     $this->assertRaw(t('Disabled text format %format.', array('%format' => $edit['name'])), 'Format successfully disabled.');
     // Allow authenticated users on full HTML.
     $format = entity_load('filter_format', $full);
     $edit = array();
     $edit['roles[' . RoleInterface::ANONYMOUS_ID . ']'] = 0;
     $edit['roles[' . RoleInterface::AUTHENTICATED_ID . ']'] = 1;
     $this->drupalPostForm('admin/config/content/formats/manage/' . $full, $edit, t('Save configuration'));
     $this->assertUrl('admin/config/content/formats');
     $this->assertRaw(t('The text format %format has been updated.', array('%format' => $format->label())), 'Full HTML format successfully updated.');
     // Switch user.
     $this->drupalLogin($this->webUser);
     $this->drupalGet('node/add/page');
     $this->assertRaw('<option value="' . $full . '">Full HTML</option>', 'Full HTML filter accessible.');
     // Use basic HTML and see if it removes tags that are not allowed.
     $body = '<em>' . $this->randomMachineName() . '</em>';
     $extra_text = 'text';
     $text = $body . '<random>' . $extra_text . '</random>';
     $edit = array();
     $edit['title[0][value]'] = $this->randomMachineName();
     $edit['body[0][value]'] = $text;
     $edit['body[0][format]'] = $basic;
     $this->drupalPostForm('node/add/page', $edit, t('Save'));
     $this->assertRaw(t('Basic page %title has been created.', array('%title' => $edit['title[0][value]'])), 'Filtered node created.');
     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
     $this->assertTrue($node, 'Node found in database.');
     $this->drupalGet('node/' . $node->id());
     $this->assertRaw($body . $extra_text, 'Filter removed invalid tag.');
     // Use plain text and see if it escapes all tags, whether allowed or not.
     // In order to test plain text, we have to enable the hidden variable for
     // "show_fallback_format", which displays plain text in the format list.
     $this->config('filter.settings')->set('always_show_fallback_choice', TRUE)->save();
     $edit = array();
     $edit['body[0][format]'] = $plain;
     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
     $this->drupalGet('node/' . $node->id());
     $this->assertText(SafeMarkup::checkPlain($text), 'The "Plain text" text format escapes all HTML tags.');
     $this->config('filter.settings')->set('always_show_fallback_choice', FALSE)->save();
     // Switch user.
     $this->drupalLogin($this->adminUser);
     // Clean up.
     // Allowed tags.
     $edit = array();
     $edit['filters[filter_html][settings][allowed_html]'] = '<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>';
     $this->drupalPostForm('admin/config/content/formats/manage/' . $basic, $edit, t('Save configuration'));
     $this->assertUrl('admin/config/content/formats');
     $this->drupalGet('admin/config/content/formats/manage/' . $basic);
     $this->assertFieldByName('filters[filter_html][settings][allowed_html]', $edit['filters[filter_html][settings][allowed_html]'], 'Changes reverted.');
     // Full HTML.
     $edit = array();
     $edit['roles[' . RoleInterface::AUTHENTICATED_ID . ']'] = FALSE;
     $this->drupalPostForm('admin/config/content/formats/manage/' . $full, $edit, t('Save configuration'));
     $this->assertUrl('admin/config/content/formats');
     $this->assertRaw(t('The text format %format has been updated.', array('%format' => $format->label())), 'Full HTML format successfully reverted.');
     $this->drupalGet('admin/config/content/formats/manage/' . $full);
     $this->assertFieldByName('roles[' . RoleInterface::AUTHENTICATED_ID . ']', $edit['roles[' . RoleInterface::AUTHENTICATED_ID . ']'], 'Changes reverted.');
     // Filter order.
     $edit = array();
     $edit['filters[' . $second_filter . '][weight]'] = 2;
     $edit['filters[' . $first_filter . '][weight]'] = 1;
     $this->drupalPostForm('admin/config/content/formats/manage/' . $basic, $edit, t('Save configuration'));
     $this->assertUrl('admin/config/content/formats');
     $this->drupalGet('admin/config/content/formats/manage/' . $basic);
     $this->assertFieldByName('filters[' . $second_filter . '][weight]', $edit['filters[' . $second_filter . '][weight]'], 'Changes reverted.');
     $this->assertFieldByName('filters[' . $first_filter . '][weight]', $edit['filters[' . $first_filter . '][weight]'], 'Changes reverted.');
 }
Пример #7
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     parent::submitForm($form, $form_state);
     filter_formats_reset();
     drupal_set_message($this->t('The text format ordering has been saved.'));
 }