You are here

public function OnlyOneModuleHandlerTest::testGetModuleHelpPageLink in Allow a content type only once (Only One) 8

Tests the returned link from OnlyOneModuleHandler::getModuleHelpPageLink().

@covers ::getModuleHelpPageLink @dataProvider providerGetModuleHelpPageLink

Parameters

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

string $module_machine_name: The module machine name.

string $module_name_alternate: Alternate module name to use if the module is not present in the site.

string|false $emphasize: Use this parameter to wrap with <em> tags the module name if the module is not installed or not present in the site.

File

tests/src/Unit/OnlyOneModuleHandlerTest.php, line 82

Class

OnlyOneModuleHandlerTest
Tests the OnlyOneModuleHandler class methods.

Namespace

Drupal\Tests\onlyone\Unit

Code

public function testGetModuleHelpPageLink($expected, $module_machine_name, $module_name_alternate, $emphasize = FALSE) {

  // All the installed modules.
  $modules = [];
  $modules['admin_toolbar']['name'] = 'Admin Toolbar';
  $modules['modules_weight']['name'] = 'Modules Weight';
  $modules['no_autocomplete']['name'] = 'No Autocomplete';
  $modules['node']['name'] = 'Node';
  $modules['views']['name'] = 'Views';
  $modules['workflows']['name'] = 'Workflows';

  // Mocking getAllInstalledInfo method.
  $this->moduleExtensionList
    ->expects($this
    ->any())
    ->method('getAllInstalledInfo')
    ->willReturn($modules);

  // Modules installed and implementing the hook_help.
  $modules_with_hook_help = [
    'admin_toolbar',
    'modules_weight',
    'no_autocomplete',
  ];

  // Mocking getImplementations method.
  $this->moduleHandler
    ->expects($this
    ->any())
    ->method('getImplementations')
    ->with('help')
    ->willReturn($modules_with_hook_help);

  // Here $module_name_alternate made the works as $module_name.
  $build = [
    '#type' => 'link',
    '#title' => $module_name_alternate,
    '#url' => Url::fromRoute('help.page', [
      'name' => $module_machine_name,
    ]),
    '#cache' => [
      'tags' => [
        'config:core.extension',
      ],
    ],
  ];

  // Mocking render method.
  $this->renderer
    ->expects($this
    ->any())
    ->method('render')
    ->with($build)
    ->willReturn($expected);

  // Testing the function.
  $this
    ->assertEquals($expected, $this->onlyOneModuleHandler
    ->getModuleHelpPageLink($module_machine_name, $module_name_alternate, $emphasize));
}