You are here

public function OnlyOneTest::testExistsNodesContentType in Allow a content type only once (Only One) 8

Tests the existence of nodes with OnlyOne::existsNodesContentType().

@covers ::existsNodesContentType @dataProvider providerExistsNodesContentType

Parameters

string $expected: The expected result from calling the function.

string $content_type: The content type to check.

bool $multilingual: Whether the site is multilingual or not.

string $current_language: The current language selected from the interface.

array $nids: An array of ids. The keys and values are entity ids.

File

tests/src/Unit/OnlyOneTest.php, line 218

Class

OnlyOneTest
Tests the OnlyOne class methods.

Namespace

Drupal\Tests\onlyone\Unit

Code

public function testExistsNodesContentType($expected, $content_type, $multilingual, $current_language, array $nids) {

  // QueryInterface mock.
  $query = $this
    ->createMock('\\Drupal\\Core\\Entity\\Query\\QueryInterface');

  // QueryInterface::condition mock.
  $query
    ->method('condition')
    ->withConsecutive([
    'type',
    $content_type,
  ], [
    'langcode',
    $current_language,
  ])
    ->willReturnOnConsecutiveCalls($this
    ->returnSelf(), $this
    ->returnSelf());

  // Mocking execute method.
  $query
    ->expects($this
    ->any())
    ->method('execute')
    ->willReturn($nids);

  // EntityStorage mock.
  $entity_storage = $this
    ->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');

  // Mocking getQuery.
  $entity_storage
    ->expects($this
    ->any())
    ->method('getQuery')
    ->willReturn($query);

  // Mocking getStorage method.
  $this->entityTypeManager
    ->expects($this
    ->any())
    ->method('getStorage')
    ->with('node')
    ->willReturn($entity_storage);

  // Mocking isMultilingual.
  $this->languageManager
    ->expects($this
    ->any())
    ->method('isMultilingual')
    ->willReturn($multilingual);

  // LanguageInterface mock.
  $language = $this
    ->createMock('Drupal\\Core\\Language\\LanguageInterface');

  // Mocking getId method.
  $language
    ->expects($this
    ->any())
    ->method('getId')
    ->willReturn($current_language);

  // Mocking getCurrentLanguage method.
  $this->languageManager
    ->expects($this
    ->any())
    ->method('getCurrentLanguage')
    ->willReturn($language);

  // Testing the function.
  $this
    ->assertEquals($expected, $this->onlyOne
    ->existsNodesContentType($content_type));
}