You are here

class OnlyOneAdminToolbarTest in Allow a content type only once (Only One) 8

Tests the Language class methods.

@group onlyone @group onlyone_admin_toolbar @coversDefaultClass \Drupal\onlyone_admin_toolbar\OnlyOneAdminToolbar

Hierarchy

Expanded class hierarchy of OnlyOneAdminToolbarTest

File

modules/onlyone_admin_toolbar/tests/src/Unit/OnlyOneAdminToolbarTest.php, line 15

Namespace

Drupal\Tests\onlyone_admin_toolbar\Unit
View source
class OnlyOneAdminToolbarTest extends UnitTestCase {

  /**
   * A config factory instance.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $configFactory;

  /**
   * A route builder instance.
   *
   * @var \Drupal\Core\Routing\RouteBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $routeBuilder;

  /**
   * The OnlyOneAdminToolbar Object.
   *
   * @var \Drupal\onlyone_admin_toolbar\OnlyOneAdminToolbar
   */
  protected $onlyOneAdminToolbar;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    // Config factory mock.
    $this->configFactory = $this
      ->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');

    // Route builder mock.
    $this->routeBuilder = $this
      ->createMock('Drupal\\Core\\Routing\\RouteBuilderInterface');

    // Creating the object.
    $this->onlyOneAdminToolbar = new OnlyOneAdminToolbar($this->configFactory, $this->routeBuilder);
  }

  /**
   * Tests the OnlyOneAdminToolbar::rebuildMenu() method.
   *
   * @param string $content_type
   *   Content type machine name.
   * @param array $content_types_list
   *   Array of content types machine names.
   *
   * @covers ::rebuildMenu
   * @dataProvider providerRebuildMenu
   */
  public function testRebuildMenu($content_type, array $content_types_list) {

    // ImmutableConfig mock.
    $config = $this
      ->createMock('Drupal\\Core\\Config\\ImmutableConfig');

    // ImmutableConfig::get mock.
    $config
      ->expects($this
      ->any())
      ->method('get')
      ->with('onlyone_node_types')
      ->willReturn($content_types_list);

    // Mocking get method.
    $this->configFactory
      ->expects($this
      ->any())
      ->method('get')
      ->with('onlyone.settings')
      ->willReturn($config);

    // Mocking rebuild method.
    $this->routeBuilder
      ->expects($this
      ->any())
      ->method('rebuild')
      ->willReturn(TRUE);

    // Testing the function.
    $this
      ->assertNull($this->onlyOneAdminToolbar
      ->rebuildMenu($content_type));
  }

  /**
   * Data provider for testRebuildMenu().
   *
   * @return array
   *   An array of arrays, each containing:
   *   - 'content_type' - Content type machine name.
   *   - 'content_types_list' - Array of content types machine names.
   *
   * @see testRebuildMenu()
   */
  public function providerRebuildMenu() {
    $content_types_list = [
      'page',
      'forum',
      'article',
    ];
    $tests['configured content type'] = [
      'page',
      $content_types_list,
    ];
    $tests['configured content type'] = [
      'article',
      $content_types_list,
    ];
    $tests['not configured content type'] = [
      'blog',
      $content_types_list,
    ];
    $tests['not configured content type'] = [
      'log',
      $content_types_list,
    ];
    return $tests;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OnlyOneAdminToolbarTest::$configFactory protected property A config factory instance.
OnlyOneAdminToolbarTest::$onlyOneAdminToolbar protected property The OnlyOneAdminToolbar Object.
OnlyOneAdminToolbarTest::$routeBuilder protected property A route builder instance.
OnlyOneAdminToolbarTest::providerRebuildMenu public function Data provider for testRebuildMenu().
OnlyOneAdminToolbarTest::setUp public function Overrides UnitTestCase::setUp
OnlyOneAdminToolbarTest::testRebuildMenu public function Tests the OnlyOneAdminToolbar::rebuildMenu() method.
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.