You are here

class LingotekDashboardControllerTest in Lingotek Translation 8.2

Same name and namespace in other branches
  1. 8 tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
  2. 4.0.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
  3. 3.0.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
  4. 3.1.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
  5. 3.2.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
  6. 3.3.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
  7. 3.4.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
  8. 3.5.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
  9. 3.6.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
  10. 3.7.x tests/src/Unit/Controller/LingotekDashboardControllerTest.php \Drupal\Tests\lingotek\Unit\Controller\LingotekDashboardControllerTest
  11. 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

Expanded class hierarchy of LingotekDashboardControllerTest

File

tests/src/Unit/Controller/LingotekDashboardControllerTest.php, line 26

Namespace

Drupal\Tests\lingotek\Unit\Controller
View 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 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 mocked entity storage.
   *
   * @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityStorage;

  /**
   * The controller under test.
   *
   * @var \Drupal\lingotek\Controller\LingotekDashboardController
   */
  protected $controller;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->request = $this
      ->createMock(Request::class);
    $this->configFactory = $this
      ->getConfigFactoryStub([
      'lingotek.settings' => [
        'account' => [
          'access_token' => 'at',
          'login_id' => 'login',
        ],
      ],
    ]);
    $this->entityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $this->languageManager = $this
      ->createMock(LanguageManagerInterface::class);
    $this->lingotek = $this
      ->createMock(LingotekInterface::class);
    $this->languageLocaleMapper = $this
      ->createMock(LanguageLocaleMapperInterface::class);
    $this->lingotekConfiguration = $this
      ->createMock(LingotekConfigurationServiceInterface::class);
    $this->formBuilder = $this
      ->createMock(FormBuilderInterface::class);
    $this->logger = $this
      ->createMock(LoggerInterface::class);
    $this->controller = new LingotekDashboardController($this->request, $this->configFactory, $this->entityTypeManager, $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 \Symfony\Component\HttpFoundation\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
      ->createMock(QueryInterface::class);
    $query
      ->method('condition')
      ->willReturnSelf();
    $query
      ->method('count')
      ->willReturnSelf();
    $query
      ->expects($this
      ->any())
      ->method('execute')
      ->willReturn(3);
    $this->entityStorage
      ->expects($this
      ->any())
      ->method('getQuery')
      ->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 \Symfony\Component\HttpFoundation\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
      ->createMock(ConfigurableLanguageInterface::class);
    $this->entityStorage = $this
      ->createMock(EntityStorageInterface::class);
    $this->entityStorage
      ->expects($this
      ->any())
      ->method('load')
      ->willReturn($language);
    $this->entityTypeManager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->willReturn($this->entityStorage);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LingotekDashboardControllerTest::$configFactory protected property The config factory service.
LingotekDashboardControllerTest::$controller protected property The controller under test.
LingotekDashboardControllerTest::$entityStorage protected property The mocked entity storage.
LingotekDashboardControllerTest::$entityTypeManager protected property The entity type manager.
LingotekDashboardControllerTest::$formBuilder protected property The form builder.
LingotekDashboardControllerTest::$languageLocaleMapper protected property The language-locale mapper.
LingotekDashboardControllerTest::$languageManager protected property The language manager.
LingotekDashboardControllerTest::$lingotek protected property The Lingotek service
LingotekDashboardControllerTest::$lingotekConfiguration protected property The Lingotek configuration service.
LingotekDashboardControllerTest::$logger protected property The logger channel.
LingotekDashboardControllerTest::$request protected property The mocked request.
LingotekDashboardControllerTest::setUp protected function Overrides UnitTestCase::setUp
LingotekDashboardControllerTest::setUpConfigurableLanguageMock protected function Setup the entity type manager for returning configurable language storage and its mocks.
LingotekDashboardControllerTest::testNodeTypesEnabledForLingotekTranslation public function Tests that when the node entity type is enabled, the response contains the stats of nodes.
LingotekDashboardControllerTest::testNoTypesEnabledForLingotekTranslation public function Tests that when no type is enabled, no types are included in the stats.
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.