uc_stock.test 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @file
  4. * Ubercart stock related tests
  5. */
  6. class UbercartStockTestCase extends UbercartTestHelper {
  7. public static function getInfo() {
  8. return array(
  9. 'name' => 'Stock',
  10. 'description' => 'Ensure that stock control functions properly.',
  11. 'group' => 'Ubercart',
  12. );
  13. }
  14. /**
  15. * Overrides DrupalWebTestCase::setUp().
  16. */
  17. public function setUp() {
  18. parent::setUp(array('uc_stock'), array('administer product stock'));
  19. $this->drupalLogin($this->adminUser);
  20. }
  21. public function testProductStock() {
  22. $this->drupalGet('node/' . $this->product->nid . '/edit/stock');
  23. $this->assertText($this->product->title);
  24. $this->assertText($this->product->model, 'Product SKU found.');
  25. $this->assertNoFieldChecked('edit-stock-0-active', 'Stock tracking is not active.');
  26. $this->assertFieldByName('stock[0][stock]', '0', 'Default stock level found.');
  27. $this->assertFieldByName('stock[0][threshold]', '0', 'Default stock threshold found.');
  28. $stock = rand(1, 1000);
  29. $edit = array(
  30. 'stock[0][active]' => 1,
  31. 'stock[0][stock]' => $stock,
  32. 'stock[0][threshold]' => rand(1, 100),
  33. );
  34. $this->drupalPost(NULL, $edit, t('Save changes'));
  35. $this->assertText('Stock settings saved.');
  36. $this->assertTrue(uc_stock_is_active($this->product->model));
  37. $this->assertEqual($stock, uc_stock_level($this->product->model));
  38. $stock = rand(1, 1000);
  39. uc_stock_set($this->product->model, $stock);
  40. $this->drupalGet('node/' . $this->product->nid . '/edit/stock');
  41. $this->assertFieldByName('stock[0][stock]', (string)$stock, 'Set stock level found.');
  42. }
  43. public function testStockDecrement() {
  44. $stock = rand(100, 1000);
  45. $edit = array(
  46. 'stock[0][active]' => 1,
  47. 'stock[0][stock]' => $stock,
  48. );
  49. $this->drupalPost('node/' . $this->product->nid . '/edit/stock', $edit, t('Save changes'));
  50. $this->assertText('Stock settings saved.');
  51. // Enable product quantity field.
  52. variable_set('uc_product_add_to_cart_qty', TRUE);
  53. $qty = rand(1, 100);
  54. $edit = array('qty' => $qty);
  55. $this->drupalPost('node/' . $this->product->nid, $edit, t('Add to cart'));
  56. $this->checkout();
  57. $this->assertEqual($stock - $qty, uc_stock_level($this->product->model));
  58. }
  59. public function testStockThresholdMail() {
  60. $edit = array('uc_stock_threshold_notification' => 1);
  61. $this->drupalPost('admin/store/settings/stock', $edit, 'Save configuration');
  62. $qty = rand(10, 100);
  63. $edit = array(
  64. 'stock[0][active]' => 1,
  65. 'stock[0][stock]' => $qty + 1,
  66. 'stock[0][threshold]' => $qty,
  67. );
  68. $this->drupalPost('node/' . $this->product->nid . '/edit/stock', $edit, 'Save changes');
  69. $this->drupalPost('node/' . $this->product->nid, array(), 'Add to cart');
  70. $this->checkout();
  71. $mail = $this->drupalGetMails(array('id' => 'uc_stock_threshold'));
  72. $mail = array_pop($mail);
  73. $this->assertTrue(strpos($mail['subject'], 'Stock threshold limit reached') !== FALSE, 'Threshold mail subject is correct.');
  74. $this->assertTrue(strpos($mail['body'], $this->product->title) !== FALSE, 'Mail body contains product title.');
  75. $this->assertTrue(strpos($mail['body'], $this->product->model) !== FALSE, 'Mail body contains SKU.');
  76. $this->assertTrue(strpos($mail['body'], 'has reached ' . $qty) !== FALSE, 'Mail body contains quantity.');
  77. }
  78. }