You are here

public function PriceListRepositoryTest::testDates in Commerce Pricelist 8.2

Tests dates-based loading.

@covers ::loadItem @covers ::loadItems

File

tests/src/Kernel/PriceListRepositoryTest.php, line 194

Class

PriceListRepositoryTest
Tests the price list repository.

Namespace

Drupal\Tests\commerce_pricelist\Kernel

Code

public function testDates() {
  $repository = $this->container
    ->get('commerce_pricelist.repository');
  $this->priceList
    ->setStartDate(new DrupalDateTime('2019-01-01 00:00:00'));
  $this->priceList
    ->setEndDate(new DrupalDateTime('2020-01-01 00:00:00'));
  $this->priceList
    ->save();
  $time = strtotime('2019-11-15 00:00:00');
  $context = new Context($this->user, $this->store, $time);
  $price_list_item = $repository
    ->loadItem($this->variation, 1, $context);
  $this
    ->assertEquals(new Price('5.00', 'USD'), $price_list_item
    ->getPrice());

  // Future start date.
  $this->priceList
    ->setStartDate(new DrupalDateTime('2019-12-17 00:00:00'));
  $this->priceList
    ->save();
  $context = new Context($this->user, $this->store, $time + 1);
  $price_list_item = $repository
    ->loadItem($this->variation, 1, $context);
  $this
    ->assertEmpty($price_list_item);

  // Confirm that the end date is not inclusive.
  $this->priceList
    ->setStartDate(new DrupalDateTime('2019-01-01 00:00:00'));
  $this->priceList
    ->setEndDate(new DrupalDateTime('2019-11-15 00:00:02'));
  $this->priceList
    ->save();
  $context = new Context($this->user, $this->store, $time + 2);
  $price_list_item = $repository
    ->loadItem($this->variation, 1, $context);
  $this
    ->assertEmpty($price_list_item);

  // Past end date.
  $this->priceList
    ->setStartDate(new DrupalDateTime('2018-01-01 00:00:00'));
  $this->priceList
    ->setEndDate(new DrupalDateTime('2019-01-01 00:00:00'));
  $this->priceList
    ->save();
  $context = new Context($this->user, $this->store, $time + 3);
  $price_list_item = $repository
    ->loadItem($this->variation, 1, $context);
  $this
    ->assertEmpty($price_list_item);
}