You are here

class LingotekFilterManagerUnitTest in Lingotek Translation 4.0.x

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/LingotekFilterManagerUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekFilterManagerUnitTest
  2. 3.0.x tests/src/Unit/LingotekFilterManagerUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekFilterManagerUnitTest
  3. 3.1.x tests/src/Unit/LingotekFilterManagerUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekFilterManagerUnitTest
  4. 3.2.x tests/src/Unit/LingotekFilterManagerUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekFilterManagerUnitTest
  5. 3.3.x tests/src/Unit/LingotekFilterManagerUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekFilterManagerUnitTest
  6. 3.4.x tests/src/Unit/LingotekFilterManagerUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekFilterManagerUnitTest
  7. 3.5.x tests/src/Unit/LingotekFilterManagerUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekFilterManagerUnitTest
  8. 3.6.x tests/src/Unit/LingotekFilterManagerUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekFilterManagerUnitTest
  9. 3.7.x tests/src/Unit/LingotekFilterManagerUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekFilterManagerUnitTest
  10. 3.8.x tests/src/Unit/LingotekFilterManagerUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekFilterManagerUnitTest

@coversDefaultClass \Drupal\lingotek\LingotekFilterManager @group lingotek @preserveGlobalState disabled

Hierarchy

Expanded class hierarchy of LingotekFilterManagerUnitTest

File

tests/src/Unit/LingotekFilterManagerUnitTest.php, line 16

Namespace

Drupal\Tests\lingotek\Unit
View source
class LingotekFilterManagerUnitTest extends UnitTestCase {

  /**
   * The Lingotek Filter manager.
   *
   * @var \Drupal\lingotek\LingotekFilterManagerInterface
   */
  protected $filterManager;

  /**
   * The Lingotek account settings.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $accountConfig;

  /**
   * The config object.
   *
   * @var \Drupal\Core\Config\Config|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $config;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $this->accountConfig = $this
      ->getMockBuilder(Config::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->config = $this
      ->getMockBuilder(Config::class)
      ->disableOriginalConstructor()
      ->getMock();
    $configFactory = $this
      ->createMock(ConfigFactoryInterface::class);
    $configFactory
      ->expects($this
      ->any())
      ->method('get')
      ->will($this
      ->returnCallback(function ($config_name) {
      return $config_name === 'lingotek.account' ? $this->accountConfig : $this->config;
    }));
    $this->filterManager = new LingotekFilterManager($configFactory);
  }

  /**
   * @covers ::getLocallyAvailableFilters
   */
  public function testGetLocallyAvailableFilters() {

    // Test with no local filters.
    $this->accountConfig
      ->expects($this
      ->at(0))
      ->method('get')
      ->with('resources.filter')
      ->will($this
      ->returnValue([]));
    $filters = $this->filterManager
      ->getLocallyAvailableFilters();
    $this
      ->assertNotEmpty($filters);
    $this
      ->assertArrayEquals($filters, [
      'project_default' => 'Project Default',
      'drupal_default' => 'Drupal Default',
    ]);

    // Test with some filters.
    $this->accountConfig
      ->expects($this
      ->at(0))
      ->method('get')
      ->with('resources.filter')
      ->will($this
      ->returnValue([
      'aaa' => 'Test filter',
    ]));
    $filters = $this->filterManager
      ->getLocallyAvailableFilters();
    $this
      ->assertNotEmpty($filters);
    $this
      ->assertEquals([
      'project_default' => 'Project Default',
      'drupal_default' => 'Drupal Default',
      'aaa' => 'Test filter',
    ], $filters);
  }
  public function getDefaultFilterProvider() {
    return [
      [
        'bbb',
        [
          'aaa' => 'Test label',
          'bbb' => 'Another label',
        ],
        'bbb',
      ],
      [
        'aaa',
        [
          'aaa' => 'Test label',
          'bbb' => 'Another label',
        ],
        'aaa',
      ],
      [
        'xxx',
        [
          'aaa' => 'Test label',
          'bbb' => 'Another label',
        ],
        NULL,
      ],
      [
        'xxx',
        [],
        NULL,
      ],
    ];
  }

  /**
   * @covers ::getDefaultSubfilter
   * @dataProvider getDefaultFilterProvider
   */
  public function testGetDefaultFilter($id, $filters, $expectedFilter) {
    $this->accountConfig
      ->expects($this
      ->at(0))
      ->method('get')
      ->with('default.filter')
      ->will($this
      ->returnValue($id));
    $this->accountConfig
      ->expects($this
      ->at(1))
      ->method('get')
      ->with('resources.filter')
      ->will($this
      ->returnValue($filters));
    $filter = $this->filterManager
      ->getDefaultFilter();
    $this
      ->assertEquals($expectedFilter, $filter);
  }

  /**
   * @covers ::getDefaultSubfilter
   * @dataProvider getDefaultFilterProvider
   */
  public function testGetDefaultSubfilter($id, $filters, $expectedFilter) {
    $this->accountConfig
      ->expects($this
      ->at(0))
      ->method('get')
      ->with('default.subfilter')
      ->will($this
      ->returnValue($id));
    $this->accountConfig
      ->expects($this
      ->at(1))
      ->method('get')
      ->with('resources.filter')
      ->will($this
      ->returnValue($filters));
    $filter = $this->filterManager
      ->getDefaultSubfilter();
    $this
      ->assertEquals($expectedFilter, $filter);
  }
  public function getDefaultFilterLabelProvider() {
    return [
      [
        'bbb',
        [
          'aaa' => 'Test label',
          'bbb' => 'Another label',
        ],
        'Another label',
      ],
      [
        'aaa',
        [
          'aaa' => 'Test label',
          'bbb' => 'Another label',
        ],
        'Test label',
      ],
      [
        'xxx',
        [
          'aaa' => 'Test label',
          'bbb' => 'Another label',
        ],
        '',
      ],
      [
        'xxx',
        [],
        '',
      ],
    ];
  }

  /**
   * @covers ::getDefaultFilterLabel
   * @dataProvider getDefaultFilterLabelProvider
   */
  public function testGetDefaultFilterLabel($id, $filters, $expectedLabel) {
    $this->accountConfig
      ->expects($this
      ->at(0))
      ->method('get')
      ->with('default.filter')
      ->will($this
      ->returnValue($id));
    $this->accountConfig
      ->expects($this
      ->at(1))
      ->method('get')
      ->with('resources.filter')
      ->will($this
      ->returnValue($filters));
    $this->accountConfig
      ->expects($this
      ->at(2))
      ->method('get')
      ->with('resources.filter')
      ->will($this
      ->returnValue($filters));
    $label = $this->filterManager
      ->getDefaultFilterLabel();
    $this
      ->assertEquals($expectedLabel, $label);
  }

  /**
   * @covers ::getDefaultSubfilterLabel
   * @dataProvider getDefaultFilterLabelProvider
   */
  public function testGetDefaultSubfilterLabel($id, $filters, $expectedLabel) {
    $this->accountConfig
      ->expects($this
      ->at(0))
      ->method('get')
      ->with('default.subfilter')
      ->will($this
      ->returnValue($id));
    $this->accountConfig
      ->expects($this
      ->at(1))
      ->method('get')
      ->with('resources.filter')
      ->will($this
      ->returnValue($filters));
    $this->accountConfig
      ->expects($this
      ->at(2))
      ->method('get')
      ->with('resources.filter')
      ->will($this
      ->returnValue($filters));
    $label = $this->filterManager
      ->getDefaultSubfilterLabel();
    $this
      ->assertEquals($expectedLabel, $label);
  }

  /**
   * @covers ::getSubfilterId
   */
  public function testGetFilterId() {

    // Filter id has the original value.
    $profile = new LingotekProfile([
      'id' => 'profile1',
      'project' => 'my_test_project',
      'vault' => 'my_test_vault',
      'filter' => 'my_filter',
    ], 'lingotek_profile');
    $filter = $this->filterManager
      ->getFilterId($profile);
    $this
      ->assertEquals('my_filter', $filter);

    // Filter is replaced with project default.
    $profile = new LingotekProfile([
      'id' => 'profile1',
      'project' => 'my_test_project',
      'vault' => 'my_test_vault',
      'filter' => 'project_default',
    ], 'lingotek_profile');
    $filter = $this->filterManager
      ->getFilterId($profile);
    $this
      ->assertEquals(NULL, $filter);

    // Filter is replaced with drupal default.
    $profile = new LingotekProfile([
      'id' => 'profile1',
      'project' => 'my_test_project',
      'vault' => 'my_test_vault',
      'filter' => 'drupal_default',
    ], 'lingotek_profile');
    $filter = $this->filterManager
      ->getFilterId($profile);
    $this
      ->assertEquals('4f91482b-5aa1-4a4a-a43f-712af7b39625', $filter);

    // Filter is replaced with the default.
    $this->accountConfig
      ->expects($this
      ->at(0))
      ->method('get')
      ->with('default.filter')
      ->will($this
      ->returnValue('another_different_filter'));
    $this->accountConfig
      ->expects($this
      ->at(1))
      ->method('get')
      ->with('resources.filter')
      ->will($this
      ->returnValue([
      'another_different_filter' => 'Another different filter',
    ]));
    $profile = new LingotekProfile([
      'id' => 'profile1',
      'project' => 'my_test_project',
      'vault' => 'my_test_vault',
      'filter' => 'default',
    ], 'lingotek_profile');
    $filter = $this->filterManager
      ->getFilterId($profile);
    $this
      ->assertEquals('another_different_filter', $filter);
  }

  /**
   * @covers ::getSubfilterId
   */
  public function testGetSubfilterId() {

    // Filter id has the original value.
    $profile = new LingotekProfile([
      'id' => 'profile1',
      'project' => 'my_test_project',
      'vault' => 'my_test_vault',
      'subfilter' => 'my_filter',
    ], 'lingotek_profile');
    $filter = $this->filterManager
      ->getSubfilterId($profile);
    $this
      ->assertEquals('my_filter', $filter);

    // Filter is replaced with project default.
    $profile = new LingotekProfile([
      'id' => 'profile1',
      'project' => 'my_test_project',
      'vault' => 'my_test_vault',
      'subfilter' => 'project_default',
    ], 'lingotek_profile');
    $filter = $this->filterManager
      ->getSubfilterId($profile);
    $this
      ->assertEquals(NULL, $filter);

    // Filter is replaced with drupal default.
    $profile = new LingotekProfile([
      'id' => 'profile1',
      'project' => 'my_test_project',
      'vault' => 'my_test_vault',
      'subfilter' => 'drupal_default',
    ], 'lingotek_profile');
    $filter = $this->filterManager
      ->getSubfilterId($profile);
    $this
      ->assertEquals('0e79f34d-f27b-4a0c-880e-cd9181a5d265', $filter);

    // Filter is replaced with the default.
    $this->accountConfig
      ->expects($this
      ->at(0))
      ->method('get')
      ->with('default.subfilter')
      ->will($this
      ->returnValue('another_different_filter'));
    $this->accountConfig
      ->expects($this
      ->at(1))
      ->method('get')
      ->with('resources.filter')
      ->will($this
      ->returnValue([
      'another_different_filter' => 'Another different filter',
    ]));
    $profile = new LingotekProfile([
      'id' => 'profile1',
      'project' => 'my_test_project',
      'vault' => 'my_test_vault',
      'subfilter' => 'default',
    ], 'lingotek_profile');
    $filter = $this->filterManager
      ->getSubfilterId($profile);
    $this
      ->assertEquals('another_different_filter', $filter);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LingotekFilterManagerUnitTest::$accountConfig protected property The Lingotek account settings.
LingotekFilterManagerUnitTest::$config protected property The config object.
LingotekFilterManagerUnitTest::$filterManager protected property The Lingotek Filter manager.
LingotekFilterManagerUnitTest::getDefaultFilterLabelProvider public function
LingotekFilterManagerUnitTest::getDefaultFilterProvider public function
LingotekFilterManagerUnitTest::setUp protected function Overrides UnitTestCase::setUp
LingotekFilterManagerUnitTest::testGetDefaultFilter public function @covers ::getDefaultSubfilter @dataProvider getDefaultFilterProvider
LingotekFilterManagerUnitTest::testGetDefaultFilterLabel public function @covers ::getDefaultFilterLabel @dataProvider getDefaultFilterLabelProvider
LingotekFilterManagerUnitTest::testGetDefaultSubfilter public function @covers ::getDefaultSubfilter @dataProvider getDefaultFilterProvider
LingotekFilterManagerUnitTest::testGetDefaultSubfilterLabel public function @covers ::getDefaultSubfilterLabel @dataProvider getDefaultFilterLabelProvider
LingotekFilterManagerUnitTest::testGetFilterId public function @covers ::getSubfilterId
LingotekFilterManagerUnitTest::testGetLocallyAvailableFilters public function @covers ::getLocallyAvailableFilters
LingotekFilterManagerUnitTest::testGetSubfilterId public function @covers ::getSubfilterId
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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.
UnitTestCase::setUpBeforeClass public static function