You are here

class FilterUninstallValidatorTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/filter/tests/src/Unit/FilterUninstallValidatorTest.php \Drupal\Tests\filter\Unit\FilterUninstallValidatorTest
  2. 10 core/modules/filter/tests/src/Unit/FilterUninstallValidatorTest.php \Drupal\Tests\filter\Unit\FilterUninstallValidatorTest

@coversDefaultClass \Drupal\filter\FilterUninstallValidator @group filter

Hierarchy

Expanded class hierarchy of FilterUninstallValidatorTest

File

core/modules/filter/tests/src/Unit/FilterUninstallValidatorTest.php, line 12

Namespace

Drupal\Tests\filter\Unit
View source
class FilterUninstallValidatorTest extends UnitTestCase {
  use AssertHelperTrait;

  /**
   * @var \Drupal\filter\FilterUninstallValidator|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $filterUninstallValidator;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->filterUninstallValidator = $this
      ->getMockBuilder('Drupal\\filter\\FilterUninstallValidator')
      ->disableOriginalConstructor()
      ->setMethods([
      'getFilterDefinitionsByProvider',
      'getEnabledFilterFormats',
    ])
      ->getMock();
    $this->filterUninstallValidator
      ->setStringTranslation($this
      ->getStringTranslationStub());
  }

  /**
   * @covers ::validate
   */
  public function testValidateNoPlugins() {
    $this->filterUninstallValidator
      ->expects($this
      ->once())
      ->method('getFilterDefinitionsByProvider')
      ->willReturn([]);
    $this->filterUninstallValidator
      ->expects($this
      ->never())
      ->method('getEnabledFilterFormats');
    $module = $this
      ->randomMachineName();
    $expected = [];
    $reasons = $this->filterUninstallValidator
      ->validate($module);
    $this
      ->assertSame($expected, $this
      ->castSafeStrings($reasons));
  }

  /**
   * @covers ::validate
   */
  public function testValidateNoFormats() {
    $this->filterUninstallValidator
      ->expects($this
      ->once())
      ->method('getFilterDefinitionsByProvider')
      ->willReturn([
      'test_filter_plugin' => [
        'id' => 'test_filter_plugin',
        'provider' => 'filter_test',
      ],
    ]);
    $this->filterUninstallValidator
      ->expects($this
      ->once())
      ->method('getEnabledFilterFormats')
      ->willReturn([]);
    $module = $this
      ->randomMachineName();
    $expected = [];
    $reasons = $this->filterUninstallValidator
      ->validate($module);
    $this
      ->assertSame($expected, $this
      ->castSafeStrings($reasons));
  }

  /**
   * @covers ::validate
   */
  public function testValidateNoMatchingFormats() {
    $this->filterUninstallValidator
      ->expects($this
      ->once())
      ->method('getFilterDefinitionsByProvider')
      ->willReturn([
      'test_filter_plugin1' => [
        'id' => 'test_filter_plugin1',
        'provider' => 'filter_test',
      ],
      'test_filter_plugin2' => [
        'id' => 'test_filter_plugin2',
        'provider' => 'filter_test',
      ],
      'test_filter_plugin3' => [
        'id' => 'test_filter_plugin3',
        'provider' => 'filter_test',
      ],
      'test_filter_plugin4' => [
        'id' => 'test_filter_plugin4',
        'provider' => 'filter_test',
      ],
    ]);
    $filter_plugin_enabled = $this
      ->getMockForAbstractClass('Drupal\\filter\\Plugin\\FilterBase', [
      [
        'status' => TRUE,
      ],
      '',
      [
        'provider' => 'filter_test',
      ],
    ]);
    $filter_plugin_disabled = $this
      ->getMockForAbstractClass('Drupal\\filter\\Plugin\\FilterBase', [
      [
        'status' => FALSE,
      ],
      '',
      [
        'provider' => 'filter_test',
      ],
    ]);

    // The first format has 2 matching and enabled filters, but the loop breaks
    // after finding the first one.
    $filter_plugin_collection1 = $this
      ->getMockBuilder('Drupal\\filter\\FilterPluginCollection')
      ->disableOriginalConstructor()
      ->getMock();
    $filter_plugin_collection1
      ->expects($this
      ->exactly(3))
      ->method('has')
      ->willReturnMap([
      [
        'test_filter_plugin1',
        FALSE,
      ],
      [
        'test_filter_plugin2',
        TRUE,
      ],
      [
        'test_filter_plugin3',
        TRUE,
      ],
      [
        'test_filter_plugin4',
        TRUE,
      ],
    ]);
    $filter_plugin_collection1
      ->expects($this
      ->exactly(2))
      ->method('get')
      ->willReturnMap([
      [
        'test_filter_plugin2',
        $filter_plugin_disabled,
      ],
      [
        'test_filter_plugin3',
        $filter_plugin_enabled,
      ],
      [
        'test_filter_plugin4',
        $filter_plugin_enabled,
      ],
    ]);
    $filter_format1 = $this
      ->createMock('Drupal\\filter\\FilterFormatInterface');
    $filter_format1
      ->expects($this
      ->once())
      ->method('filters')
      ->willReturn($filter_plugin_collection1);
    $filter_format1
      ->expects($this
      ->once())
      ->method('label')
      ->willReturn('Filter Format 1 Label');

    // The second filter format only has one matching and enabled filter.
    $filter_plugin_collection2 = $this
      ->getMockBuilder('Drupal\\filter\\FilterPluginCollection')
      ->disableOriginalConstructor()
      ->getMock();
    $filter_plugin_collection2
      ->expects($this
      ->exactly(4))
      ->method('has')
      ->willReturnMap([
      [
        'test_filter_plugin1',
        FALSE,
      ],
      [
        'test_filter_plugin2',
        FALSE,
      ],
      [
        'test_filter_plugin3',
        FALSE,
      ],
      [
        'test_filter_plugin4',
        TRUE,
      ],
    ]);
    $filter_plugin_collection2
      ->expects($this
      ->exactly(1))
      ->method('get')
      ->with('test_filter_plugin4')
      ->willReturn($filter_plugin_enabled);
    $filter_format2 = $this
      ->createMock('Drupal\\filter\\FilterFormatInterface');
    $filter_format2
      ->expects($this
      ->once())
      ->method('filters')
      ->willReturn($filter_plugin_collection2);
    $filter_format2
      ->expects($this
      ->once())
      ->method('label')
      ->willReturn('Filter Format 2 Label');
    $this->filterUninstallValidator
      ->expects($this
      ->once())
      ->method('getEnabledFilterFormats')
      ->willReturn([
      'test_filter_format1' => $filter_format1,
      'test_filter_format2' => $filter_format2,
    ]);
    $expected = [
      'Provides a filter plugin that is in use in the following filter formats: <em class="placeholder">Filter Format 1 Label, Filter Format 2 Label</em>',
    ];
    $reasons = $this->filterUninstallValidator
      ->validate($this
      ->randomMachineName());
    $this
      ->assertSame($expected, $this
      ->castSafeStrings($reasons));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AssertHelperTrait::castSafeStrings protected static function Casts MarkupInterface objects into strings.
FilterUninstallValidatorTest::$filterUninstallValidator protected property
FilterUninstallValidatorTest::setUp protected function Overrides UnitTestCase::setUp
FilterUninstallValidatorTest::testValidateNoFormats public function @covers ::validate
FilterUninstallValidatorTest::testValidateNoMatchingFormats public function @covers ::validate
FilterUninstallValidatorTest::testValidateNoPlugins public function @covers ::validate
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.