class LingotekDashboardControllerTest in Lingotek Translation 8
Same name and namespace in other branches
- 8.2 tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
- 4.0.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
- 3.0.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
- 3.1.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
- 3.2.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
- 3.3.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
- 3.4.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
- 3.5.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
- 3.6.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
- 3.7.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
- 3.8.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
@coversDefaultClass \Drupal\lingotek\Controller\LingotekDashboardController @group lingotek @preserveGlobalState disabled
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
Expanded class hierarchy of LingotekDashboardControllerTest
File
- tests/
src/ Unit/ Controller/ LingotekDashboardControllerTest.php, line 15
Namespace
Drupal\Tests\lingotek\Unit\ControllerView source
class LingotekDashboardControllerTest extends UnitTestCase {
/**
* The mocked request.
*
* @var \Symfony\Component\HttpFoundation\Request|\PHPUnit_Framework_MockObject_MockObject
*/
protected $request;
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $configFactory;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityTypeManager;
/**
* The mock entity query factory.
*
* @var \Drupal\Core\Entity\Query\QueryFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityQueryFactory;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $languageManager;
/**
* The Lingotek service
*
* @var \Drupal\lingotek\LingotekInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $lingotek;
/**
* The language-locale mapper.
*
* @var \Drupal\lingotek\LanguageLocaleMapperInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $languageLocaleMapper;
/**
* The Lingotek configuration service.
*
* @var \Drupal\lingotek\LingotekConfigurationServiceInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $lingotekConfiguration;
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $formBuilder;
/**
* The logger channel.
*
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $logger;
/**
* The controller under test.
*
* @var \Drupal\Lingotek\Controller\LingotekDashboardController
*/
protected $controller;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->request = $this
->getMock('Symfony\\Component\\HttpFoundation\\Request');
$this->configFactory = $this
->getConfigFactoryStub([
'lingotek.settings' => [
'account' => [
'access_token' => 'at',
'login_id' => 'login',
],
],
]);
$this->entityTypeManager = $this
->getMockBuilder('Drupal\\Core\\Entity\\EntityTypeManager')
->disableOriginalConstructor()
->getMock();
$this->entityQueryFactory = $this
->getMockBuilder('Drupal\\Core\\Entity\\Query\\QueryFactory')
->disableOriginalConstructor()
->getMock();
$this->languageManager = $this
->getMock('Drupal\\Core\\Language\\LanguageManagerInterface');
$this->lingotek = $this
->getMock('Drupal\\lingotek\\LingotekInterface');
$this->languageLocaleMapper = $this
->getMock('Drupal\\lingotek\\LanguageLocaleMapperInterface');
$this->lingotekConfiguration = $this
->getMock('Drupal\\lingotek\\LingotekConfigurationServiceInterface');
$this->formBuilder = $this
->getMock('Drupal\\Core\\Form\\FormBuilderInterface');
$this->logger = $this
->getMock('Psr\\Log\\LoggerInterface');
$this->controller = new LingotekDashboardController($this->request, $this->configFactory, $this->entityTypeManager, $this->entityQueryFactory, $this->languageManager, $this->lingotek, $this->languageLocaleMapper, $this->lingotekConfiguration, $this->formBuilder, $this->logger);
$this->controller
->setStringTranslation($this
->getStringTranslationStub());
}
/**
* Tests that when no type is enabled, no types are included in the stats.
*
* @covers ::endpoint
*/
public function testNoTypesEnabledForLingotekTranslation() {
$this
->setUpConfigurableLanguageMock();
$this->request
->expects($this
->any())
->method('getMethod')
->willReturn('GET');
$languages = [];
foreach ([
'en',
'fr',
] as $langcode) {
$language = new Language([
'id' => $langcode,
]);
$languages[$langcode] = $language;
}
$this->languageManager
->expects($this
->any())
->method('getLanguages')
->will($this
->returnValue($languages));
$this->lingotekConfiguration
->expects($this
->any())
->method('getEnabledEntityTypes')
->will($this
->returnValue([]));
$this->languageLocaleMapper
->expects($this
->any())
->method('getLocaleForLangcode')
->will($this
->returnValueMap([
[
'en',
'en_US',
],
[
'fr',
'fr_CA',
],
]));
/** @var JsonResponse $value */
$response = $this->controller
->endpoint($this->request);
$content = json_decode($response
->getContent(), TRUE);
$this
->assertEquals('GET', $content['method']);
$this
->assertEquals(2, count($content['languages']));
$this
->assertEquals(0, count($content['languages']['en_US']['source']['types']));
$this
->assertEquals(0, count($content['languages']['fr_CA']['source']['types']));
}
/**
* Tests that when the node entity type is enabled, the response contains the
* stats of nodes.
*
* @covers ::endpoint
*/
public function testNodeTypesEnabledForLingotekTranslation() {
$this
->setUpConfigurableLanguageMock();
$this->request
->expects($this
->any())
->method('getMethod')
->willReturn('GET');
$languages = [];
foreach ([
'en',
'fr',
] as $langcode) {
$language = new Language([
'id' => $langcode,
]);
$languages[$langcode] = $language;
}
$query = $this
->getMock('Drupal\\Core\\Entity\\Query\\QueryInterface');
$query
->method('condition')
->willReturnSelf();
$query
->method('count')
->willReturnSelf();
$query
->expects($this
->any())
->method('execute')
->willReturn(3);
$this->entityQueryFactory
->expects($this
->any())
->method('get')
->willReturn($query);
$this->languageManager
->expects($this
->any())
->method('getLanguages')
->will($this
->returnValue($languages));
$this->lingotekConfiguration
->expects($this
->any())
->method('getEnabledEntityTypes')
->will($this
->returnValue([
'node' => 'node',
]));
$this->languageLocaleMapper
->expects($this
->any())
->method('getLocaleForLangcode')
->will($this
->returnValueMap([
[
'en',
'en_US',
],
[
'fr',
'fr_CA',
],
]));
/** @var JsonResponse $value */
$response = $this->controller
->endpoint($this->request);
$content = json_decode($response
->getContent(), TRUE);
$this
->assertEquals('GET', $content['method']);
$this
->assertEquals(2, count($content['languages']));
$this
->assertEquals(3, $content['languages']['en_US']['source']['types']['node']);
$this
->assertEquals(1, count($content['languages']['en_US']['source']['types']));
$this
->assertEquals(3, $content['languages']['en_US']['target']['types']['node']);
$this
->assertEquals(1, count($content['languages']['en_US']['target']['types']));
$this
->assertEquals(3, $content['languages']['fr_CA']['source']['types']['node']);
$this
->assertEquals(1, count($content['languages']['fr_CA']['source']['types']));
$this
->assertEquals(3, $content['languages']['fr_CA']['target']['types']['node']);
$this
->assertEquals(1, count($content['languages']['fr_CA']['target']['types']));
$this
->assertEquals(6, $content['source']['types']['node']);
$this
->assertEquals(6, $content['target']['types']['node']);
$this
->assertEquals(6, $content['source']['total']);
$this
->assertEquals(6, $content['target']['total']);
}
/**
* Setup the entity type manager for returning configurable language storage
* and its mocks.
*/
protected function setUpConfigurableLanguageMock() {
$language = $this
->getMock('Drupal\\language\\ConfigurableLanguageInterface');
$storage = $this
->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$storage
->expects($this
->any())
->method('load')
->willReturn($language);
$this->entityTypeManager
->expects($this
->any())
->method('getStorage')
->willReturn($storage);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LingotekDashboardControllerTest:: |
protected | property | The config factory service. | |
LingotekDashboardControllerTest:: |
protected | property | The controller under test. | |
LingotekDashboardControllerTest:: |
protected | property | The mock entity query factory. | |
LingotekDashboardControllerTest:: |
protected | property | The entity type manager. | |
LingotekDashboardControllerTest:: |
protected | property | The form builder. | |
LingotekDashboardControllerTest:: |
protected | property | The language-locale mapper. | |
LingotekDashboardControllerTest:: |
protected | property | The language manager. | |
LingotekDashboardControllerTest:: |
protected | property | The Lingotek service | |
LingotekDashboardControllerTest:: |
protected | property | The Lingotek configuration service. | |
LingotekDashboardControllerTest:: |
protected | property | The logger channel. | |
LingotekDashboardControllerTest:: |
protected | property | The mocked request. | |
LingotekDashboardControllerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
LingotekDashboardControllerTest:: |
protected | function | Setup the entity type manager for returning configurable language storage and its mocks. | |
LingotekDashboardControllerTest:: |
public | function | Tests that when the node entity type is enabled, the response contains the stats of nodes. | |
LingotekDashboardControllerTest:: |
public | function | Tests that when no type is enabled, no types are included in the stats. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |