ZoneItemTest.php 1.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
<?php

namespace Drupal\Tests\address\Kernel;

use CommerceGuys\Addressing\Zone\Zone;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;

/**
 * Tests the address_zone field.
 *
 * @group commerce
 */
class ZoneItemTest extends EntityKernelTestBase {

  /**
   * @var array
   */
  public static $modules = [
    'address',
  ];

  /**
   * The test entity.
   *
   * @var \Drupal\entity_test\Entity\EntityTest
   */
  protected $testEntity;

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

    $field_storage = FieldStorageConfig::create([
      'field_name' => 'field_zone',
      'entity_type' => 'entity_test',
      'type' => 'address_zone',
      'cardinality' => 1,
    ]);
    $field_storage->save();

    $field = FieldConfig::create([
      'field_name' => 'field_zone',
      'entity_type' => 'entity_test',
      'bundle' => 'entity_test',
    ]);
    $field->save();

    $entity = EntityTest::create([
      'name' => 'Test',
    ]);
    $entity->save();
    $this->testEntity = $entity;
  }

  /**
   * Tests storing and retrieving a zone from the field.
   */
  public function testZone() {
    $zone = new Zone([
      'id' => 'test',
      'label' => 'Test',
      'territories' => [
        ['country_code' => 'HU'],
        ['country_code' => 'RS'],
      ],
    ]);
    $this->testEntity->field_zone = $zone;
    $this->testEntity->save();

    $this->testEntity = $this->reloadEntity($this->testEntity);
    $this->assertEquals($zone, $this->testEntity->field_zone->value);
  }

}