You are here

public function ListPaymentTypesTest::testExecute in Payment 8.2

@covers ::execute

File

tests/src/Unit/Controller/ListPaymentTypesTest.php, line 94

Class

ListPaymentTypesTest
@coversDefaultClass \Drupal\payment\Controller\ListPaymentTypes

Namespace

Drupal\Tests\payment\Unit\Controller

Code

public function testExecute() {
  $definitions = [
    'foo' => [
      'label' => $this
        ->randomMachineName(),
      'description' => $this
        ->randomMachineName(),
    ],
    'bar' => [
      'label' => $this
        ->randomMachineName(),
    ],
    'payment_unavailable' => [],
  ];
  $operations_foo = [
    'baz' => [
      'title' => $this
        ->randomMachineName(),
    ],
  ];
  $operations_provider_foo = $this
    ->createMock(PluginOperationsProviderInterface::class);
  $operations_provider_foo
    ->expects($this
    ->once())
    ->method('getOperations')
    ->with('foo')
    ->willReturn($operations_foo);
  $this->paymentTypeManager
    ->expects($this
    ->once())
    ->method('getDefinitions')
    ->willReturn($definitions);
  $map = [
    [
      'foo',
      $operations_provider_foo,
    ],
    [
      'bar',
      NULL,
    ],
  ];
  $this->paymentTypeManager
    ->expects($this
    ->exactly(2))
    ->method('getOperationsProvider')
    ->willReturnMap($map);
  $this->moduleHandler
    ->expects($this
    ->any())
    ->method('moduleExists')
    ->with('field_ui')
    ->willReturn(TRUE);
  $map = [
    [
      'administer payment fields',
      TRUE,
    ],
    [
      'administer payment form display',
      TRUE,
    ],
    [
      'administer payment display',
      TRUE,
    ],
  ];
  $this->currentUser
    ->expects($this
    ->atLeastOnce())
    ->method('hasPermission')
    ->willReturnMap($map);
  $build = $this->sut
    ->execute();
  $expected_build = [
    '#empty' => 'There are no available payment types.',
    '#header' => [
      'Type',
      'Description',
      'Operations',
    ],
    '#type' => 'table',
    'foo' => [
      'label' => [
        '#markup' => $definitions['foo']['label'],
      ],
      'description' => [
        '#markup' => $definitions['foo']['description'],
      ],
      'operations' => [
        '#links' => $operations_foo + [
          'configure' => [
            'url' => new Url('payment.payment_type', [
              'bundle' => 'foo',
            ]),
            'title' => 'Configure',
          ],
          'manage-fields' => [
            'title' => 'Manage fields',
            'url' => new Url('entity.payment.field_ui_fields', [
              'bundle' => 'foo',
            ]),
          ],
          'manage-form-display' => [
            'title' => 'Manage form display',
            'url' => new Url('entity.entity_form_display.payment.default', [
              'bundle' => 'foo',
            ]),
          ],
          'manage-display' => [
            'title' => 'Manage display',
            'url' => new Url('entity.entity_view_display.payment.default', [
              'bundle' => 'foo',
            ]),
          ],
        ],
        '#type' => 'operations',
      ],
    ],
    'bar' => [
      'label' => [
        '#markup' => $definitions['bar']['label'],
      ],
      'description' => [
        '#markup' => NULL,
      ],
      'operations' => [
        '#links' => [
          'configure' => [
            'url' => new Url('payment.payment_type', [
              'bundle' => 'bar',
            ]),
            'title' => 'Configure',
          ],
          'manage-fields' => [
            'title' => 'Manage fields',
            'url' => new Url('entity.payment.field_ui_fields', [
              'bundle' => 'bar',
            ]),
          ],
          'manage-form-display' => [
            'title' => 'Manage form display',
            'url' => new Url('entity.entity_form_display.payment.default', [
              'bundle' => 'bar',
            ]),
          ],
          'manage-display' => [
            'title' => 'Manage display',
            'url' => new Url('entity.entity_view_display.payment.default', [
              'bundle' => 'bar',
            ]),
          ],
        ],
        '#type' => 'operations',
      ],
    ],
  ];
  $this
    ->assertEquals($expected_build, $build);
}