FilterCrudTest.php 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\filter\Tests\FilterCrudTest.
 */

namespace Drupal\filter\Tests;

10
use Drupal\simpletest\KernelTestBase;
11 12

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

  /**
   * Modules to enable.
   *
   * @var array
   */
24
  public static $modules = array('filter', 'filter_test');
25

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

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

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

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

    // Disable the text format.
70
    $format->disable()->save();
71 72

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

  /**
77
   * Verifies that a text format is properly stored.
78 79
   */
  function verifyTextFormat($format) {
80
    $t_args = array('%format' => $format->label());
81
    $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
82

83
    // Verify the loaded filter has all properties.
84 85 86 87
    $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));
88
    // Check that the filter was created in site default language.
89
    $this->assertEqual($format->language()->getId(), $default_langcode, format_string('filter_format_load: Proper language code for text format %format.', $t_args));
90 91 92
  }

}