You are here

class ChecklistapiModuleTest in Checklist API 8

Tests the functions in checklistapi.module.

@group checklistapi

Hierarchy

Expanded class hierarchy of ChecklistapiModuleTest

File

tests/src/Unit/ChecklistapiModuleTest.php, line 18

Namespace

Drupal\Tests\checklistapi\Unit
View source
class ChecklistapiModuleTest extends UnitTestCase {

  /**
   * Tests checklistapi_sort_array().
   */
  public function testChecklistapiSortArray() {
    $input = [
      '#title' => 'Checklist API test',
      '#path' => 'admin/config/development/checklistapi-test',
      // @codingStandardsIgnoreLine
      '#description' => 'A test checklist.',
      '#help' => '<p>This is a test checklist.</p>',
      'group_two' => [
        '#title' => 'Group two',
      ],
      'group_one' => [
        '#title' => 'Group one',
        // @codingStandardsIgnoreLine
        '#description' => '<p>Group one description.</p>',
        '#weight' => -1,
        'item_three' => [
          '#title' => 'Item three',
          '#weight' => 1,
        ],
        'item_one' => [
          '#title' => 'Item one',
          // @codingStandardsIgnoreLine
          '#description' => 'Item one description',
          '#weight' => -1,
          'link_three' => [
            '#text' => 'Link three',
            '#url' => Url::fromUri('http://example.com/three'),
            '#weight' => 3,
          ],
          'link_two' => [
            '#text' => 'Link two',
            '#url' => Url::fromUri('http://example.com/two'),
            '#weight' => 2,
          ],
          'link_one' => [
            '#text' => 'Link one',
            '#url' => Url::fromUri('http://example.com/one'),
            '#weight' => 1,
          ],
        ],
        'item_two' => [
          '#title' => 'Item two',
        ],
      ],
      'group_four' => [
        '#title' => 'Group four',
        '#weight' => 1,
      ],
      'group_three' => [
        '#title' => 'Group three',
      ],
    ];
    $output = checklistapi_sort_array($input);
    $this
      ->assertEquals(0, $output['group_two']['#weight'], 'Failed to supply a default for omitted element weight.');
    $this
      ->assertEquals(0, $output['group_three']['#weight'], 'Failed to supply a default in place of invalid element weight.');
    $this
      ->assertEquals(-1, $output['group_one']['#weight'], 'Failed to retain a valid element weight.');
    $this
      ->assertEquals([
      'group_one',
      'group_two',
      'group_three',
      'group_four',
    ], Element::children($output), 'Failed to sort elements by weight.');
    $this
      ->assertEquals([
      'link_one',
      'link_two',
      'link_three',
    ], Element::children($output['group_one']['item_one']), 'Failed to recurse through element descendants.');
  }

  /**
   * Tests checklistapi_strtolowercamel().
   */
  public function testChecklistapiStrtolowercamel() {
    $this
      ->assertEquals('abcDefGhi', checklistapi_strtolowercamel('Abc def_ghi'), 'Failed to convert string to lowerCamel case.');
  }

  /**
   * Tests that checklistapi_checklist_access() rejects an invalid mode.
   */
  public function testChecklistapiChecklistAccessInvalidMode() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage('No such operation "invalid operation');
    checklistapi_checklist_access(NULL, 'invalid operation');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ChecklistapiModuleTest::testChecklistapiChecklistAccessInvalidMode public function Tests that checklistapi_checklist_access() rejects an invalid mode.
ChecklistapiModuleTest::testChecklistapiSortArray public function Tests checklistapi_sort_array().
ChecklistapiModuleTest::testChecklistapiStrtolowercamel public function Tests checklistapi_strtolowercamel().
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.
UnitTestCase::setUp protected function 340