You are here

public function ShippingMethodFormatterTest::testRender in Commerce Shipping 8.2

Tests the rendered output.

@covers ::viewElements

File

tests/src/Kernel/Formatter/ShippingMethodFormatterTest.php, line 51

Class

ShippingMethodFormatterTest
Tests the shipping method formatter.

Namespace

Drupal\Tests\commerce_shipping\Kernel\Formatter

Code

public function testRender() {

  /** @var \Drupal\commerce_shipping\Entity\ShippingMethodInterface $shipping_method */
  $shipping_method = ShippingMethod::create([
    'name' => $this
      ->randomString(),
    'status' => 1,
    'plugin' => [
      'target_plugin_id' => 'flat_rate',
      'target_plugin_configuration' => [
        'rate_label' => 'Test shipping',
        'rate_amount' => [
          'amount' => '10',
          'currency_code' => 'USD',
        ],
        'services' => [
          'default',
        ],
      ],
    ],
  ]);
  $shipping_method
    ->addTranslation($this->translationLanguage
    ->id(), [
    'name' => 'Shipping method',
    'plugin' => [
      'target_plugin_id' => 'flat_rate',
      'target_plugin_configuration' => [
        'rate_label' => 'Test translation shipping',
        'rate_amount' => [
          'number' => '10',
          'currency_code' => 'USD',
        ],
      ],
    ],
  ]);
  $shipping_method
    ->save();
  $shipment = Shipment::create([
    'type' => 'default',
    // The order ID is invalid, but this test doesn't need to care about that.
    'order_id' => '6',
    'shipping_method' => $shipping_method
      ->id(),
    'shipping_service' => 'default',
    'title' => 'Shipment #1',
    'state' => 'ready',
  ]);
  $shipment
    ->save();
  $default_language = $this->container
    ->get('language_manager')
    ->getDefaultLanguage();
  $this->container
    ->get('language.default')
    ->set($this->translationLanguage);

  // Confirm that the translated rate_label is used.
  $view_display = EntityViewDisplay::collectRenderDisplay($shipment, 'default');
  $build = $view_display
    ->build($shipment);
  $this
    ->render($build);
  $this
    ->assertText('Shipping method');
  $this
    ->assertText('Test translation shipping');

  // Revert the site default language to the original language.
  $this->container
    ->get('language.default')
    ->set($default_language);
  $this->container
    ->get('language_manager')
    ->reset();
  $view_display = EntityViewDisplay::collectRenderDisplay($shipment, 'default');
  $build = $view_display
    ->build($shipment);
  $this
    ->render($build);
  $this
    ->assertText('Shipping method');
  $this
    ->assertText('Test shipping');

  // Confirm that deleting the shipping method doesn't crash the formatter.
  $shipping_method
    ->delete();
  $shipment = $this
    ->reloadEntity($shipment);
  $build = $view_display
    ->build($shipment);
  $this
    ->render($build);
  $this
    ->assertNoText('Shipping method', $this->content);
  $this
    ->assertNoText('Test shipping');
  $this
    ->assertNoText('Test translation shipping');
}