class OnlyOneModuleHandlerTest in Allow a content type only once (Only One) 8
Tests the OnlyOneModuleHandler class methods.
@group onlyone @coversDefaultClass \Drupal\onlyone\OnlyOneModuleHandler
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait- class \Drupal\Tests\onlyone\Unit\OnlyOneModuleHandlerTest
 
Expanded class hierarchy of OnlyOneModuleHandlerTest
File
- tests/src/ Unit/ OnlyOneModuleHandlerTest.php, line 15 
Namespace
Drupal\Tests\onlyone\UnitView source
class OnlyOneModuleHandlerTest extends UnitTestCase {
  /**
   * A module handler instance.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $moduleHandler;
  /**
   * A renderer instance.
   *
   * @var \Drupal\Core\Render\RendererInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $renderer;
  /**
   * A module extension list instance.
   *
   * @var \Drupal\Core\Extension\ModuleExtensionList|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $moduleExtensionList;
  /**
   * The OnlyOneModuleHandler Object.
   *
   * @var \Drupal\onlyone\OnlyOneModuleHandler
   */
  protected $onlyOneModuleHandler;
  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    if (!defined('DRUPAL_MINIMUM_PHP')) {
      define('DRUPAL_MINIMUM_PHP', '5.5.9');
    }
    // Module Handler mock.
    $this->moduleHandler = $this
      ->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
    // Renderer mock.
    $this->renderer = $this
      ->createMock('Drupal\\Core\\Render\\RendererInterface');
    // ModuleExtensionList mock.
    $this->moduleExtensionList = $this
      ->createMock('Drupal\\Core\\Extension\\ModuleExtensionList');
    // Creating the object.
    $this->onlyOneModuleHandler = new OnlyOneModuleHandler($this->moduleHandler, $this->renderer, $this->moduleExtensionList);
  }
  /**
   * Tests the returned link from OnlyOneModuleHandler::getModuleHelpPageLink().
   *
   * @param string $expected
   *   The expected result from calling the function.
   * @param string $module_machine_name
   *   The module machine name.
   * @param string $module_name_alternate
   *   Alternate module name to use if the module is not present in the site.
   * @param 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.
   *
   * @covers ::getModuleHelpPageLink
   * @dataProvider providerGetModuleHelpPageLink
   */
  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));
  }
  /**
   * Data provider for testGetModuleHelpPageLink().
   *
   * @return array
   *   An array of arrays, each containing:
   *   - 'expected' - Expected return from existsNodesContentType().
   *   - 'module_machine_name' - The module machine name.
   *   - 'module_name_alternate' - Alternate module name.
   *   - 'emphasize' - Boolean for wrap with <em> tags the module name.
   *
   * @see testExistsNodesContentType()
   */
  public function providerGetModuleHelpPageLink() {
    $tests = [
      // No existing modules.
      [
        '<em>Action</em>',
        'action',
        'Action',
        TRUE,
      ],
      [
        'Asana Module',
        'asana',
        'Asana Module',
        FALSE,
      ],
      [
        'Webform',
        'webform',
        'Webform',
      ],
      // Existing installed modules.
      [
        'Admin Toolbar',
        'admin_toolbar',
        'Admin Toolbar',
        TRUE,
      ],
      [
        'Modules Weight',
        'modules_weight',
        'Modules Weight',
      ],
      [
        'No Autocomplete',
        'no_autocomplete',
        'No Autocomplete',
        FALSE,
      ],
      // Existing not installed modules.
      [
        '<em>Node</em>',
        'node',
        'Node Module',
        TRUE,
      ],
      [
        'Views',
        'views',
        'Views Module',
        FALSE,
      ],
      [
        'Workflows',
        'workflows',
        'Workflows Module',
      ],
    ];
    return $tests;
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| OnlyOneModuleHandlerTest:: | protected | property | A module extension list instance. | |
| OnlyOneModuleHandlerTest:: | protected | property | A module handler instance. | |
| OnlyOneModuleHandlerTest:: | protected | property | The OnlyOneModuleHandler Object. | |
| OnlyOneModuleHandlerTest:: | protected | property | A renderer instance. | |
| OnlyOneModuleHandlerTest:: | public | function | Data provider for testGetModuleHelpPageLink(). | |
| OnlyOneModuleHandlerTest:: | public | function | Overrides UnitTestCase:: | |
| OnlyOneModuleHandlerTest:: | public | function | Tests the returned link from OnlyOneModuleHandler::getModuleHelpPageLink(). | |
| 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. | 
