FilterDefaultFormatTest.php 3.15 KB
Newer Older
1 2 3 4
<?php

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

namespace Drupal\filter\Tests;

10
use Drupal\Component\Utility\Unicode;
11 12
use Drupal\simpletest\WebTestBase;

13
/**
14 15 16
 * Tests the default text formats for different users.
 *
 * @group filter
17
 */
18
class FilterDefaultFormatTest extends WebTestBase {
19 20 21 22 23 24 25 26

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

27 28 29
  /**
   * Tests if the default text format is accessible to users.
   */
30 31 32 33 34 35 36 37
  function testDefaultTextFormats() {
    // Create two text formats, and two users. The first user has access to
    // both formats, but the second user only has access to the second one.
    $admin_user = $this->drupalCreateUser(array('administer filters'));
    $this->drupalLogin($admin_user);
    $formats = array();
    for ($i = 0; $i < 2; $i++) {
      $edit = array(
38
        'format' => Unicode::strtolower($this->randomMachineName()),
39
        'name' => $this->randomMachineName(),
40
      );
41
      $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
42
      $this->resetFilterCaches();
43
      $formats[] = entity_load('filter_format', $edit['format']);
44 45
    }
    list($first_format, $second_format) = $formats;
46 47 48
    $second_format_permission = $second_format->getPermissionName();
    $first_user = $this->drupalCreateUser(array($first_format->getPermissionName(), $second_format_permission));
    $second_user = $this->drupalCreateUser(array($second_format_permission));
49 50 51 52

    // Adjust the weights so that the first and second formats (in that order)
    // are the two lowest weighted formats available to any user.
    $edit = array();
53 54
    $edit['formats[' . $first_format->id() . '][weight]'] = -2;
    $edit['formats[' . $second_format->id() . '][weight]'] = -1;
55
    $this->drupalPostForm('admin/config/content/formats', $edit, t('Save changes'));
56 57 58 59
    $this->resetFilterCaches();

    // Check that each user's default format is the lowest weighted format that
    // the user has access to.
60
    $actual = filter_default_format($first_user);
61
    $expected = $first_format->id();
62 63
    $this->assertEqual($actual, $expected, "First user's default format $actual is the expected lowest weighted format $expected that the user has access to.");
    $actual = filter_default_format($second_user);
64
    $expected = $second_format->id();
65
    $this->assertEqual($actual, $expected, "Second user's default format $actual is the expected lowest weighted format $expected that the user has access to, and different to the first user's.");
66 67 68 69

    // Reorder the two formats, and check that both users now have the same
    // default.
    $edit = array();
70
    $edit['formats[' . $second_format->id() . '][weight]'] = -3;
71
    $this->drupalPostForm('admin/config/content/formats', $edit, t('Save changes'));
72
    $this->resetFilterCaches();
73
    $this->assertEqual(filter_default_format($first_user), filter_default_format($second_user), 'After the formats are reordered, both users have the same default format.');
74 75 76
  }

  /**
77
   * Rebuilds text format and permission caches in the thread running the tests.
78 79 80 81 82
   */
  protected function resetFilterCaches() {
    filter_formats_reset();
  }
}