public function OrderTest::testOrder in Commerce Core 8.2
Tests the order entity and its methods.
@covers ::getOrderNumber @covers ::setOrderNumber @covers ::getStore @covers ::setStore @covers ::getStoreId @covers ::setStoreId @covers ::getCustomer @covers ::setCustomer @covers ::getCustomerId @covers ::setCustomerId @covers ::getEmail @covers ::setEmail @covers ::getIpAddress @covers ::setIpAddress @covers ::getBillingProfile @covers ::setBillingProfile @covers ::collectProfiles @covers ::getItems @covers ::setItems @covers ::hasItems @covers ::addItem @covers ::removeItem @covers ::hasItem @covers ::getAdjustments @covers ::setAdjustments @covers ::addAdjustment @covers ::removeAdjustment @covers ::clearAdjustments @covers ::collectAdjustments @covers ::getSubtotalPrice @covers ::recalculateTotalPrice @covers ::getTotalPrice @covers ::getTotalPaid @covers ::setTotalPaid @covers ::getBalance @covers ::isPaid @covers ::getState @covers ::getRefreshState @covers ::setRefreshState @covers ::getData @covers ::setData @covers ::unsetData @covers ::isLocked @covers ::lock @covers ::unlock @covers ::getCreatedTime @covers ::setCreatedTime @covers ::getPlacedTime @covers ::setPlacedTime @covers ::getCompletedTime @covers ::setCompletedTime @covers ::getCalculationDate
File
- modules/
order/ tests/ src/ Kernel/ Entity/ OrderTest.php, line 105
Class
- OrderTest
- Tests the Order entity.
Namespace
Drupal\Tests\commerce_order\Kernel\EntityCode
public function testOrder() {
/** @var \Drupal\profile\Entity\ProfileInterface $profile */
$profile = Profile::create([
'type' => 'customer',
'address' => [
'country_code' => 'US',
'postal_code' => '53177',
'locality' => 'Milwaukee',
'address_line1' => 'Pabst Blue Ribbon Dr',
'administrative_area' => 'WI',
'given_name' => 'Frederick',
'family_name' => 'Pabst',
],
]);
$profile
->save();
$profile = $this
->reloadEntity($profile);
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
$order_item = OrderItem::create([
'type' => 'test',
'quantity' => '1',
'unit_price' => new Price('2.00', 'USD'),
]);
$order_item
->save();
$order_item = $this
->reloadEntity($order_item);
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $another_order_item */
$another_order_item = OrderItem::create([
'type' => 'test',
'quantity' => '2',
'unit_price' => new Price('3.00', 'USD'),
]);
$another_order_item
->save();
$another_order_item = $this
->reloadEntity($another_order_item);
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = Order::create([
'type' => 'default',
'state' => 'completed',
'store_id' => $this->store
->id(),
]);
$order
->save();
$order
->setOrderNumber(7);
$this
->assertEquals(7, $order
->getOrderNumber());
$this
->assertFalse($order
->isPaid());
$order
->setStore($this->store);
$this
->assertEquals($this->store, $order
->getStore());
$this
->assertEquals($this->store
->id(), $order
->getStoreId());
$order
->setStoreId(0);
$this
->assertEquals(NULL, $order
->getStore());
$order
->setStoreId($this->store
->id());
$this
->assertEquals($this->store, $order
->getStore());
$this
->assertEquals($this->store
->id(), $order
->getStoreId());
$this
->assertInstanceOf(UserInterface::class, $order
->getCustomer());
$this
->assertTrue($order
->getCustomer()
->isAnonymous());
$this
->assertEquals(0, $order
->getCustomerId());
$order
->setCustomer($this->user);
$this
->assertEquals($this->user, $order
->getCustomer());
$this
->assertEquals($this->user
->id(), $order
->getCustomerId());
$this
->assertTrue($order
->getCustomer()
->isAuthenticated());
// Non-existent/deleted user ID.
$order
->setCustomerId(888);
$this
->assertInstanceOf(UserInterface::class, $order
->getCustomer());
$this
->assertTrue($order
->getCustomer()
->isAnonymous());
$this
->assertEquals(888, $order
->getCustomerId());
$order
->setCustomerId($this->user
->id());
$this
->assertEquals($this->user, $order
->getCustomer());
$this
->assertEquals($this->user
->id(), $order
->getCustomerId());
$order
->setEmail('commerce@example.com');
$this
->assertEquals('commerce@example.com', $order
->getEmail());
$order
->setIpAddress('127.0.0.2');
$this
->assertEquals('127.0.0.2', $order
->getIpAddress());
$order
->setBillingProfile($profile);
$this
->assertEquals($profile, $order
->getBillingProfile());
$profiles = $order
->collectProfiles();
$this
->assertCount(1, $profiles);
$this
->assertArrayHasKey('billing', $profiles);
$this
->assertEquals($profile, $profiles['billing']);
$order
->setItems([
$order_item,
$another_order_item,
]);
$this
->assertEquals([
$order_item,
$another_order_item,
], $order
->getItems());
$this
->assertNotEmpty($order
->hasItems());
$order
->removeItem($another_order_item);
$this
->assertEquals([
$order_item,
], $order
->getItems());
$this
->assertNotEmpty($order
->hasItem($order_item));
$this
->assertEmpty($order
->hasItem($another_order_item));
$order
->addItem($another_order_item);
$this
->assertEquals([
$order_item,
$another_order_item,
], $order
->getItems());
$this
->assertNotEmpty($order
->hasItem($another_order_item));
$this
->assertEquals(new Price('8.00', 'USD'), $order
->getTotalPrice());
$adjustments = [];
$adjustments[] = new Adjustment([
'type' => 'custom',
'label' => '10% off',
'amount' => new Price('-1.00', 'USD'),
]);
$adjustments[] = new Adjustment([
'type' => 'fee',
'label' => 'Handling fee',
'amount' => new Price('10.00', 'USD'),
'locked' => TRUE,
]);
$order
->addAdjustment($adjustments[0]);
$order
->addAdjustment($adjustments[1]);
$this
->assertEquals($adjustments, $order
->getAdjustments());
$this
->assertEquals($adjustments, $order
->getAdjustments([
'custom',
'fee',
]));
$this
->assertEquals([
$adjustments[0],
], $order
->getAdjustments([
'custom',
]));
$this
->assertEquals([
$adjustments[1],
], $order
->getAdjustments([
'fee',
]));
$order
->removeAdjustment($adjustments[0]);
$this
->assertEquals(new Price('8.00', 'USD'), $order
->getSubtotalPrice());
$this
->assertEquals(new Price('18.00', 'USD'), $order
->getTotalPrice());
$this
->assertEquals([
$adjustments[1],
], $order
->getAdjustments());
$order
->setAdjustments($adjustments);
$this
->assertEquals($adjustments, $order
->getAdjustments());
$this
->assertEquals(new Price('17.00', 'USD'), $order
->getTotalPrice());
// Confirm that locked adjustments persist after clear.
// Custom adjustments are locked by default.
$order
->addAdjustment(new Adjustment([
'type' => 'fee',
'label' => 'Random fee',
'amount' => new Price('10.00', 'USD'),
]));
$order
->clearAdjustments();
$this
->assertEquals($adjustments, $order
->getAdjustments());
$this
->assertEquals($adjustments, $order
->collectAdjustments());
$this
->assertEquals($adjustments, $order
->collectAdjustments([
'custom',
'fee',
]));
$this
->assertEquals([
$adjustments[0],
], $order
->collectAdjustments([
'custom',
]));
$this
->assertEquals([
$adjustments[1],
], $order
->collectAdjustments([
'fee',
]));
$this
->assertEquals(new Price('0', 'USD'), $order
->getTotalPaid());
$this
->assertEquals(new Price('17.00', 'USD'), $order
->getBalance());
$this
->assertFalse($order
->isPaid());
$order
->setTotalPaid(new Price('7.00', 'USD'));
$this
->assertEquals(new Price('7.00', 'USD'), $order
->getTotalPaid());
$this
->assertEquals(new Price('10.00', 'USD'), $order
->getBalance());
$this
->assertFalse($order
->isPaid());
$order
->setTotalPaid(new Price('17.00', 'USD'));
$this
->assertEquals(new Price('17.00', 'USD'), $order
->getTotalPaid());
$this
->assertEquals(new Price('0', 'USD'), $order
->getBalance());
$this
->assertTrue($order
->isPaid());
$order
->setTotalPaid(new Price('27.00', 'USD'));
$this
->assertEquals(new Price('27.00', 'USD'), $order
->getTotalPaid());
$this
->assertEquals(new Price('-10.00', 'USD'), $order
->getBalance());
$this
->assertTrue($order
->isPaid());
$this
->assertEquals('completed', $order
->getState()
->getId());
// Confirm that free orders are considered paid after placement.
$order
->addAdjustment(new Adjustment([
'type' => 'custom',
'label' => '100% off',
'amount' => new Price('-17.00', 'USD'),
]));
$order
->setTotalPaid(new Price('0', 'USD'));
$this
->assertTrue($order
->getTotalPrice()
->isZero());
$this
->assertTrue($order
->isPaid());
$order
->set('state', 'draft');
$this
->assertFalse($order
->isPaid());
$order
->setRefreshState(Order::REFRESH_ON_SAVE);
$this
->assertEquals(Order::REFRESH_ON_SAVE, $order
->getRefreshState());
$this
->assertEquals('default', $order
->getData('test', 'default'));
$order
->setData('test', 'value');
$this
->assertEquals('value', $order
->getData('test', 'default'));
$order
->unsetData('test');
$this
->assertNull($order
->getData('test'));
$this
->assertEquals('default', $order
->getData('test', 'default'));
$this
->assertFalse($order
->isLocked());
$order
->lock();
$this
->assertTrue($order
->isLocked());
$order
->unlock();
$this
->assertFalse($order
->isLocked());
$order
->setCreatedTime(635879700);
$this
->assertEquals(635879700, $order
->getCreatedTime());
$order
->setPlacedTime(635879800);
$this
->assertEquals(635879800, $order
->getPlacedTime());
$order
->setCompletedTime(635879900);
$this
->assertEquals(635879900, $order
->getCompletedTime());
$date = $order
->getCalculationDate();
$this
->assertEquals($order
->getPlacedTime(), $date
->format('U'));
$order
->set('placed', NULL);
$date = $order
->getCalculationDate();
$this
->assertEquals(\Drupal::time()
->getRequestTime(), $date
->format('U'));
}