View source
<?php
namespace Drupal\Tests\commerce_shipping\Kernel\Formatter;
use Drupal\commerce_shipping\Entity\Shipment;
use Drupal\commerce_shipping\Entity\ShippingMethod;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\commerce_shipping\Kernel\ShippingKernelTestBase;
class ShippingMethodFormatterTest extends ShippingKernelTestBase {
public static $modules = [
'language',
'content_translation',
];
protected $translationLanguage;
protected function setUp() : void {
parent::setUp();
$this->translationLanguage = ConfigurableLanguage::createFromLangcode('fr');
$this->translationLanguage
->save();
$this->container
->get('content_translation.manager')
->setEnabled('commerce_shipping_method', 'commerce_shipping_method', TRUE);
}
public function testRender() {
$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',
'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);
$view_display = EntityViewDisplay::collectRenderDisplay($shipment, 'default');
$build = $view_display
->build($shipment);
$this
->render($build);
$this
->assertText('Shipping method');
$this
->assertText('Test translation shipping');
$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');
$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');
}
}