View source
<?php
namespace Drupal\Tests\uc_stock\Functional;
use Drupal\Tests\uc_store\Functional\UbercartBrowserTestBase;
class StockTest extends UbercartBrowserTestBase {
protected static $modules = [
'uc_stock',
];
protected static $adminPermissions = [
'administer product stock',
];
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('page_title_block');
\Drupal::configFactory()
->getEditable('system.mail')
->set('interface.uc_stock', 'test_mail_collector')
->save();
$this
->drupalLogin($this->adminUser);
}
public function testProductStock() {
$assert = $this
->assertSession();
$sku = $this->product->model->value;
$prefix = 'stock[' . $sku . ']';
$this
->drupalGet('node/' . $this->product
->id() . '/edit/stock');
$assert
->pageTextContains($this->product
->label());
$assert
->pageTextContains($this->product->model->value);
$assert
->checkboxNotChecked('edit-stock-' . strtolower($sku) . '-active');
$assert
->fieldValueEquals($prefix . '[stock]', '0');
$assert
->fieldValueEquals($prefix . '[threshold]', '0');
$stock = rand(1, 1000);
$edit = [
$prefix . '[active]' => 1,
$prefix . '[stock]' => $stock,
$prefix . '[threshold]' => rand(1, 100),
];
$this
->submitForm($edit, 'Save changes');
$assert
->pageTextContains('Stock settings saved.');
$this
->assertTrue(uc_stock_is_active($sku));
$this
->assertEquals($stock, uc_stock_level($sku));
$stock = rand(1, 1000);
uc_stock_set($sku, $stock);
$this
->drupalGet('node/' . $this->product
->id() . '/edit/stock');
$assert
->fieldValueEquals($prefix . '[stock]', (string) $stock);
}
public function testStockDecrement() {
$assert = $this
->assertSession();
$prefix = 'stock[' . $this->product->model->value . ']';
$stock = rand(100, 1000);
$edit = [
$prefix . '[active]' => 1,
$prefix . '[stock]' => $stock,
];
$this
->drupalGet('node/' . $this->product
->id() . '/edit/stock');
$this
->submitForm($edit, 'Save changes');
$assert
->pageTextContains('Stock settings saved.');
$edit = [
'uc_product_add_to_cart_qty' => TRUE,
];
$this
->drupalGet('admin/store/config/products');
$this
->submitForm($edit, 'Save configuration');
$qty = rand(1, 100);
$edit = [
'qty' => $qty,
];
$this
->addToCart($this->product, $edit);
$this
->checkout();
$this
->assertEquals($stock - $qty, uc_stock_level($this->product->model->value));
}
public function testStockThresholdMail() {
$prefix = 'stock[' . $this->product->model->value . ']';
$edit = [
'uc_stock_threshold_notification' => 1,
];
$this
->drupalGet('admin/store/config/stock');
$this
->submitForm($edit, 'Save configuration');
$qty = rand(10, 100);
$edit = [
$prefix . '[active]' => 1,
$prefix . '[stock]' => $qty + 1,
$prefix . '[threshold]' => $qty,
];
$this
->drupalGet('node/' . $this->product
->id() . '/edit/stock');
$this
->submitForm($edit, 'Save changes');
$this
->addToCart($this->product);
$this
->checkout();
$mail = $this
->getMails([
'id' => 'uc_stock_threshold',
]);
$mail = array_pop($mail);
$this
->assertEquals(uc_store_email(), $mail['to'], 'Threshold mail recipient is correct.');
$this
->assertNotSame(FALSE, strpos($mail['subject'], 'Stock threshold limit reached'), 'Threshold mail subject is correct.');
$this
->assertNotSame(FALSE, strpos($mail['body'], $this->product
->label()), 'Mail body contains product title.');
$this
->assertNotSame(FALSE, strpos($mail['body'], $this->product->model->value), 'Mail body contains SKU.');
$this
->assertNotSame(FALSE, strpos($mail['body'], 'has reached ' . $qty), 'Mail body contains quantity.');
}
public function testStockChangeAfterEditingOrder() {
$assert = $this
->assertSession();
$prefix = 'stock[' . $this->product->model->value . ']';
$stock = rand(100, 1000);
$edit = [
$prefix . '[active]' => 1,
$prefix . '[stock]' => $stock,
];
$this
->drupalGet('node/' . $this->product
->id() . '/edit/stock');
$this
->submitForm($edit, 'Save changes');
$edit = [
'uc_product_add_to_cart_qty' => TRUE,
];
$this
->drupalGet('admin/store/config/products');
$this
->submitForm($edit, 'Save configuration');
$qty = rand(1, 50);
$edit = [
'qty' => $qty,
];
$this
->addToCart($this->product, $edit);
$order = $this
->checkout();
$this
->drupalGet('admin/store/orders/' . $order
->id() . '/edit');
$order_products = $order->products;
reset($order_products);
$order_product_id = key($order_products);
$assert
->fieldValueEquals('products[' . $order_product_id . '][qty]', (string) $qty);
$increased_qty = $qty + rand(1, 50);
$edit = [
'products[' . $order_product_id . '][qty]' => $increased_qty,
];
$this
->drupalGet('admin/store/orders/' . $order
->id() . '/edit');
$this
->submitForm($edit, 'Save changes');
$assert
->fieldValueEquals('products[' . $order_product_id . '][qty]', (string) $increased_qty);
$this
->assertEquals($stock - $increased_qty, uc_stock_level($this->product->model->value));
}
}