FilterCrudTest.php 3.44 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\filter\Tests\FilterCrudTest.
6 7 8 9
 */

namespace Drupal\filter\Tests;

10
use Drupal\filter\Entity\FilterFormat;
11
use Drupal\simpletest\KernelTestBase;
12 13

/**
14 15 16
 * Tests creation, loading, updating, deleting of text formats and filters.
 *
 * @group filter
17
 */
18
class FilterCrudTest extends KernelTestBase {
19 20 21 22 23 24

  /**
   * Modules to enable.
   *
   * @var array
   */
25
  public static $modules = ['filter', 'filter_test', 'system', 'user'];
26

27
  /**
28
   * Tests CRUD operations for text formats and filters.
29 30 31
   */
  function testTextFormatCrud() {
    // Add a text format with minimum data only.
32 33 34 35
    $format = FilterFormat::create(array(
      'format' => 'empty_format',
      'name' => 'Empty format',
    ));
36
    $format->save();
37 38 39
    $this->verifyTextFormat($format);

    // Add another text format specifying all possible properties.
40
    $format = FilterFormat::create(array(
41 42 43
      'format' => 'custom_format',
      'name' => 'Custom format',
    ));
44 45 46 47
    $format->setFilterConfig('filter_url', array(
      'status' => 1,
      'settings' => array(
        'filter_url_length' => 30,
48
      ),
49
    ));
50
    $format->save();
51 52 53
    $this->verifyTextFormat($format);

    // Alter some text format properties and save again.
54
    $format->set('name', 'Altered format');
55 56 57 58 59 60
    $format->setFilterConfig('filter_url', array(
      'status' => 0,
    ));
    $format->setFilterConfig('filter_autop', array(
      'status' => 1,
    ));
61
    $format->save();
62 63
    $this->verifyTextFormat($format);

64 65
    // Add a filter_test_replace  filter and save again.
    $format->setFilterConfig('filter_test_replace', array(
66 67
      'status' => 1,
    ));
68
    $format->save();
69 70 71
    $this->verifyTextFormat($format);

    // Disable the text format.
72
    $format->disable()->save();
73 74

    $formats = filter_formats();
75
    $this->assertTrue(!isset($formats[$format->id()]), 'filter_formats: Disabled text format no longer exists.');
76 77
  }

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  /**
   * 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);
    }
  }

93
  /**
94
   * Verifies that a text format is properly stored.
95 96
   */
  function verifyTextFormat($format) {
97
    $t_args = array('%format' => $format->label());
98
    $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
99

100
    // Verify the loaded filter has all properties.
101 102 103 104
    $filter_format = entity_load('filter_format', $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));
105
    // Check that the filter was created in site default language.
106
    $this->assertEqual($format->language()->getId(), $default_langcode, format_string('filter_format_load: Proper language code for text format %format.', $t_args));
107 108 109
  }

}