View source
<?php
namespace Drupal\Tests\lingotek\Unit;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\lingotek\Entity\LingotekProfile;
use Drupal\lingotek\LingotekFilterManager;
use Drupal\Tests\UnitTestCase;
class LingotekFilterManagerUnitTest extends UnitTestCase {
protected $filterManager;
protected $config;
protected function setUp() : void {
$this->config = $this
->getMockBuilder(Config::class)
->disableOriginalConstructor()
->getMock();
$configFactory = $this
->createMock(ConfigFactoryInterface::class);
$configFactory
->expects($this
->any())
->method('get')
->with('lingotek.settings')
->will($this
->returnValue($this->config));
$this->filterManager = new LingotekFilterManager($configFactory);
}
public function testGetLocallyAvailableFilters() {
$this->config
->expects($this
->at(0))
->method('get')
->with('account.resources.filter')
->will($this
->returnValue([]));
$filters = $this->filterManager
->getLocallyAvailableFilters();
$this
->assertNotEmpty($filters);
$this
->assertArrayEquals($filters, [
'project_default' => 'Project Default',
'drupal_default' => 'Drupal Default',
]);
$this->config
->expects($this
->at(0))
->method('get')
->with('account.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,
],
];
}
public function testGetDefaultFilter($id, $filters, $expectedFilter) {
$this->config
->expects($this
->at(0))
->method('get')
->with('default.filter')
->will($this
->returnValue($id));
$this->config
->expects($this
->at(1))
->method('get')
->with('account.resources.filter')
->will($this
->returnValue($filters));
$filter = $this->filterManager
->getDefaultFilter();
$this
->assertEquals($expectedFilter, $filter);
}
public function testGetDefaultSubfilter($id, $filters, $expectedFilter) {
$this->config
->expects($this
->at(0))
->method('get')
->with('default.subfilter')
->will($this
->returnValue($id));
$this->config
->expects($this
->at(1))
->method('get')
->with('account.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',
[],
'',
],
];
}
public function testGetDefaultFilterLabel($id, $filters, $expectedLabel) {
$this->config
->expects($this
->at(0))
->method('get')
->with('default.filter')
->will($this
->returnValue($id));
$this->config
->expects($this
->at(1))
->method('get')
->with('account.resources.filter')
->will($this
->returnValue($filters));
$this->config
->expects($this
->at(2))
->method('get')
->with('account.resources.filter')
->will($this
->returnValue($filters));
$label = $this->filterManager
->getDefaultFilterLabel();
$this
->assertEquals($expectedLabel, $label);
}
public function testGetDefaultSubfilterLabel($id, $filters, $expectedLabel) {
$this->config
->expects($this
->at(0))
->method('get')
->with('default.subfilter')
->will($this
->returnValue($id));
$this->config
->expects($this
->at(1))
->method('get')
->with('account.resources.filter')
->will($this
->returnValue($filters));
$this->config
->expects($this
->at(2))
->method('get')
->with('account.resources.filter')
->will($this
->returnValue($filters));
$label = $this->filterManager
->getDefaultSubfilterLabel();
$this
->assertEquals($expectedLabel, $label);
}
public function testGetFilterId() {
$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);
$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);
$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);
$this->config
->expects($this
->at(0))
->method('get')
->with('default.filter')
->will($this
->returnValue('another_different_filter'));
$this->config
->expects($this
->at(1))
->method('get')
->with('account.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);
}
public function testGetSubfilterId() {
$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);
$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);
$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);
$this->config
->expects($this
->at(0))
->method('get')
->with('default.subfilter')
->will($this
->returnValue('another_different_filter'));
$this->config
->expects($this
->at(1))
->method('get')
->with('account.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);
}
}