1 filter.test FilterDefaultFormatTestCase::testDefaultTextFormats()

Tests if the default text format is accessible to users.

File

core/modules/filter/tests/filter.test, line 854
Tests for filter.module.

Class

FilterDefaultFormatTestCase
Tests the default filter functionality in the Filter module.

Code

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->backdropCreateUser(array('administer filters'));
  $this->backdropLogin($admin_user);
  $formats = array();
  for ($i = 0; $i < 2; $i++) {
    $edit = array(
      'format' => backdrop_strtolower($this->randomName()),
      'name' => $this->randomName(),
    );
    $this->backdropPost('admin/config/content/formats/add', $edit, t('Save configuration'));
    $this->resetFilterCaches();
    $formats[] = filter_format_load($edit['format']);
  }
  list($first_format, $second_format) = $formats;
  $first_user = $this->backdropCreateUser(array(filter_permission_name($first_format), filter_permission_name($second_format)));
  $second_user = $this->backdropCreateUser(array(filter_permission_name($second_format)));

  // 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();
  $edit['formats[' . $first_format->format . '][weight]'] = '-2';
  $edit['formats[' . $second_format->format . '][weight]'] = '-1';
  $this->backdropPost('admin/config/content/formats', $edit, t('Save changes'));
  $this->resetFilterCaches();

  // Check that each user's default format is the lowest weighted format that
  // the user has access to.
  $this->assertEqual(filter_default_format($first_user), $first_format->format, "The first user's default format is the lowest weighted format that the user has access to.");
  $this->assertEqual(filter_default_format($second_user), $second_format->format, "The second user's default format is the lowest weighted format that the user has access to, and is different than the first user's.");

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