Skip to content
FilterCrudTest.php 3.33 KiB
Newer Older
use Drupal\filter\Entity\FilterFormat;
use Drupal\KernelTests\KernelTestBase;
 * Tests creation, loading, updating, deleting of text formats and filters.
 *
 * @group filter
class FilterCrudTest extends KernelTestBase {
  public static $modules = ['filter', 'filter_test', 'system', 'user'];
   * Tests CRUD operations for text formats and filters.
   */
  function testTextFormatCrud() {
    // Add a text format with minimum data only.
      'format' => 'empty_format',
      'name' => 'Empty format',
    $this->verifyTextFormat($format);

    // Add another text format specifying all possible properties.
      'format' => 'custom_format',
      'name' => 'Custom format',
    ]);
    $format->setFilterConfig('filter_url', [
    $this->verifyTextFormat($format);

    // Alter some text format properties and save again.
    $format->set('name', 'Altered format');
    $format->setFilterConfig('filter_url', [
    ]);
    $format->setFilterConfig('filter_autop', [
    $this->verifyTextFormat($format);

    // Add a filter_test_replace  filter and save again.
    $format->setFilterConfig('filter_test_replace', [
    $this->verifyTextFormat($format);

    // Disable the text format.
    $this->assertTrue(!isset($formats[$format->id()]), 'filter_formats: Disabled text format no longer exists.');
  /**
   * Tests disabling the fallback text format.
   */
  public function testDisableFallbackFormat() {
    $this->installConfig(['filter']);
    $message = '\LogicException with message "The fallback text format \'plain_text\' cannot be disabled." was thrown.';
    try {
      FilterFormat::load('plain_text')->disable();
      $this->fail($message);
    }
    catch (\LogicException $e) {
      $this->assertIdentical($e->getMessage(), "The fallback text format 'plain_text' cannot be disabled.", $message);
    }
  }

   * Verifies that a text format is properly stored.
   */
  function verifyTextFormat($format) {
    $t_args = ['%format' => $format->label()];
    $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
    // Verify the loaded filter has all properties.
    $filter_format = FilterFormat::load($format->id());
    $this->assertEqual($filter_format->id(), $format->id(), format_string('filter_format_load: Proper format id for text format %format.', $t_args));
    $this->assertEqual($filter_format->label(), $format->label(), format_string('filter_format_load: Proper title for text format %format.', $t_args));
    $this->assertEqual($filter_format->get('weight'), $format->get('weight'), format_string('filter_format_load: Proper weight for text format %format.', $t_args));
    // Check that the filter was created in site default language.
    $this->assertEqual($format->language()->getId(), $default_langcode, format_string('filter_format_load: Proper language code for text format %format.', $t_args));