You are here

StockTest.php in Ubercart 8.4

File

uc_stock/tests/src/Functional/StockTest.php
View source
<?php

namespace Drupal\Tests\uc_stock\Functional;

use Drupal\Tests\uc_store\Functional\UbercartBrowserTestBase;

/**
 * Tests the stock control functionality.
 *
 * @group ubercart
 */
class StockTest extends UbercartBrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'uc_stock',
  ];

  /**
   * {@inheritdoc}
   */
  protected static $adminPermissions = [
    'administer product stock',
  ];

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

    // Need page_title_block because we test page titles.
    $this
      ->drupalPlaceBlock('page_title_block');

    // Ensure test mails are logged.
    \Drupal::configFactory()
      ->getEditable('system.mail')
      ->set('interface.uc_stock', 'test_mail_collector')
      ->save();
    $this
      ->drupalLogin($this->adminUser);
  }

  /**
   * Tests stock settings on product edit page.
   */
  public function testProductStock() {

    /** @var \Drupal\Tests\WebAssert $assert */
    $assert = $this
      ->assertSession();
    $sku = $this->product->model->value;
    $prefix = 'stock[' . $sku . ']';
    $this
      ->drupalGet('node/' . $this->product
      ->id() . '/edit/stock');
    $assert
      ->pageTextContains($this->product
      ->label());

    // Check for SKU on product edit page.
    $assert
      ->pageTextContains($this->product->model->value);

    // Ensure stock tracking is not active.
    $assert
      ->checkboxNotChecked('edit-stock-' . strtolower($sku) . '-active');

    // Verify that default stock level found.
    $assert
      ->fieldValueEquals($prefix . '[stock]', '0');

    // Verify that default stock threshold found.
    $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');

    // Verify that set stock level found.
    $assert
      ->fieldValueEquals($prefix . '[stock]', (string) $stock);
  }

  /**
   * Tests stock decrementing.
   */
  public function testStockDecrement() {

    /** @var \Drupal\Tests\WebAssert $assert */
    $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.');

    // Enable product quantity field.
    $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));
  }

  /**
   * Tests sending out mail when stock drops below threshold.
   */
  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.');
  }

  /**
   * Tests stock increment/decrement when admin edits order.
   */
  public function testStockChangeAfterEditingOrder() {

    /** @var \Drupal\Tests\WebAssert $assert */
    $assert = $this
      ->assertSession();

    // Set stock level.
    $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');

    // Enable product quantity field.
    $edit = [
      'uc_product_add_to_cart_qty' => TRUE,
    ];
    $this
      ->drupalGet('admin/store/config/products');
    $this
      ->submitForm($edit, 'Save configuration');

    // Add the product to the cart and place an order.
    $qty = rand(1, 50);
    $edit = [
      'qty' => $qty,
    ];
    $this
      ->addToCart($this->product, $edit);
    $order = $this
      ->checkout();

    // Go to the order page.
    $this
      ->drupalGet('admin/store/orders/' . $order
      ->id() . '/edit');

    // Get the first OrderProduct entity's id.
    $order_products = $order->products;
    reset($order_products);
    $order_product_id = key($order_products);

    // Verify that product's quantity is found.
    $assert
      ->fieldValueEquals('products[' . $order_product_id . '][qty]', (string) $qty);

    // Increase the quantity of the product on order edit form.
    $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');

    // Verify the product's quantity is updated.
    $assert
      ->fieldValueEquals('products[' . $order_product_id . '][qty]', (string) $increased_qty);
    $this
      ->assertEquals($stock - $increased_qty, uc_stock_level($this->product->model->value));
  }

}

Classes

Namesort descending Description
StockTest Tests the stock control functionality.