Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IsSaleableTest.php
Go to the documentation of this file.
1 <?php
8 
14 class IsSaleableTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $objectManager;
20 
24  protected $productRepository;
25 
26  protected function setUp()
27  {
29  $this->productRepository = $this->objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
30  }
31 
39  {
40  $bundleProduct = $this->productRepository->get('bundle-product');
41  $bundleProduct->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
42 
43  $this->assertTrue(
44  $bundleProduct->isSalable(),
45  'Bundle product supposed to be saleable
46  if his status is enabled'
47  );
48  }
49 
57  {
58  $bundleProduct = $this->productRepository->get('bundle-product');
59  $bundleProduct->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
60 
61  $this->assertFalse(
62  $bundleProduct->isSalable(),
63  'Bundle product supposed to be non saleable
64  if his status is disabled'
65  );
66  }
67 
76  {
77  $bundleProduct = $this->productRepository->get('bundle-product');
78  $bundleProduct->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
79  $bundleProduct->setData('is_salable', true);
80 
81  $this->assertTrue(
82  $bundleProduct->isSalable(),
83  'Bundle product supposed to be saleable
84  if his status is enabled and it has data is_salable = true'
85  );
86  }
87 
96  {
97  $bundleProduct = $this->productRepository->get('bundle-product');
98  $bundleProduct->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
99  $bundleProduct->setData('is_salable', false);
100 
101  $this->assertFalse(
102  $bundleProduct->isSalable(),
103  'Bundle product supposed to be non saleable
104  if his status is enabled but his data is_salable = false'
105  );
106  }
107 
115  {
116  $bundleProduct = $this->productRepository->get('bundle-product');
117  $bundleProduct->setData('all_items_salable', true);
118 
119  $this->assertTrue(
120  $bundleProduct->isSalable(),
121  'Bundle product supposed to be saleable
122  if it has data all_items_salable = true'
123  );
124  }
125 
133  {
134  $bundleProduct = $this->productRepository->get('bundle-product');
135  $bundleProduct->setData('all_items_salable', false);
136 
137  $this->assertFalse(
138  $bundleProduct->isSalable(),
139  'Bundle product supposed to be non saleable
140  if it has data all_items_salable = false'
141  );
142  }
143 
151  {
152  $optionRepository = $this->objectManager->create(\Magento\Bundle\Api\ProductOptionRepositoryInterface::class);
153  $bundleProduct = $this->productRepository->get('bundle-product');
154 
155  // TODO: make cleaner option deletion after fix MAGETWO-59465
156  $ea = $bundleProduct->getExtensionAttributes();
157  foreach ($ea->getBundleProductOptions() as $option) {
158  $optionRepository->delete($option);
159  }
160  $ea->setBundleProductOptions([]);
161  $bundleProduct->setExtensionAttributes($ea);
162 
163  $bundleProduct = $this->productRepository->save($bundleProduct);
164 
165  $this->assertFalse(
166  $bundleProduct->isSalable(),
167  'Bundle product supposed to be non saleable
168  if it has no options'
169  );
170  }
171 
178  public function testIsSaleableOnBundleWithoutSelections()
179  {
180  $bundleProduct = $this->productRepository->get('bundle-product', true, null, true);
181  $bundleType = $bundleProduct->getTypeInstance();
183  $linkManager = $this->objectManager->create(\Magento\Bundle\Model\LinkManagement::class);
184 
186  $options = $bundleType->getOptionsCollection($bundleProduct);
187  $selections = $bundleType->getSelectionsCollection($options->getAllIds(), $bundleProduct);
188 
189  foreach ($selections as $link) {
191  $linkManager->removeChild('bundle-product', $link->getOptionId(), $link->getSku());
192  }
193 
194  $bundleProduct = $this->productRepository->get('bundle-product', false, null, true);
195  $this->assertFalse(
196  $bundleProduct->isSalable(),
197  'Bundle product supposed to be non saleable
198  if it has no selections'
199  );
200  }
201 
210  {
211  $productsSku = ['simple1', 'simple2', 'simple3', 'simple4', 'simple5'];
212  foreach ($productsSku as $productSku) {
213  $product = $this->productRepository->get($productSku);
214  $product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
215  $this->productRepository->save($product);
216  }
217 
218  $bundleProduct = $this->productRepository->get('bundle-product');
219 
220  $this->assertFalse(
221  $bundleProduct->isSalable(),
222  'Bundle product supposed to be non saleable
223  if all his selections are not saleable'
224  );
225  }
226 
235  {
236  $productsSku = ['simple1', 'simple2', 'simple3'];
237  foreach ($productsSku as $productSku) {
238  $product = $this->productRepository->get($productSku);
239  $product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
240  $this->productRepository->save($product);
241  }
242 
243  $bundleProduct = $this->productRepository->get('bundle-product');
244 
245  $this->assertFalse(
246  $bundleProduct->isSalable(),
247  'Bundle product supposed to be non saleable
248  if it has at least one required option with no saleable selections'
249  );
250  }
251 
260  {
261  $this->setQtyForSelections(['simple1', 'simple2', 'simple3'], 1);
262 
263  $bundleProduct = $this->productRepository->get('bundle-product');
264 
265  $this->assertFalse(
266  $bundleProduct->isSalable(),
267  'Bundle product supposed to be non saleable
268  if there are not enough qty of selections on required options'
269  );
270  }
271 
280  {
281  $this->setQtyForSelections(['simple1', 'simple2', 'simple3', 'simple4', 'simple5'], 1);
282  $bundleProduct = $this->productRepository->get('bundle-product');
283  $options = $bundleProduct->getExtensionAttributes()->getBundleProductOptions();
284 
285  foreach ($options as $productOption) {
286  $links = $productOption->getProductLinks();
287  foreach ($links as $link) {
288  $link->setSelectionCanChangeQuantity(1);
289  }
290 
291  $productOption->setProductLinks($links);
292  }
293 
294  $extension = $bundleProduct->getExtensionAttributes();
295  $extension->setBundleProductOptions($options);
296  $bundleProduct->setExtensionAttributes($extension);
297 
298  $bundleProduct = $this->productRepository->save($bundleProduct);
299 
300  $this->assertTrue(
301  $bundleProduct->isSalable(),
302  'Bundle product supposed to be saleable
303  if all his selections have selection_can_change_qty = 1'
304  );
305  }
306 
315  {
316  // making selections as not saleable
317  $productsSku = ['simple1', 'simple2', 'simple3', 'simple4', 'simple5'];
318  foreach ($productsSku as $productSku) {
319  $product = $this->productRepository->get($productSku);
320  $product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
321  $this->productRepository->save($product);
322  }
323 
324  $bundleProduct = $this->productRepository->get('bundle-product');
325 
326  // setting all options as not required
327  $options = $bundleProduct->getExtensionAttributes()->getBundleProductOptions();
328  foreach ($options as $productOption) {
329  $productOption->setRequired(false);
330  }
331 
332  $extension = $bundleProduct->getExtensionAttributes();
333  $extension->setBundleProductOptions($options);
334  $bundleProduct->setExtensionAttributes($extension);
335  $bundleProduct = $this->productRepository->save($bundleProduct);
336 
337  $this->assertFalse(
338  $bundleProduct->isSalable(),
339  'Bundle product supposed to be not saleable
340  if all his options are not required and selections are not saleable'
341  );
342  }
343 
352  {
353  // making selections as not saleable except simple3
354  $productsSku = ['simple1', 'simple2', 'simple4', 'simple5'];
355 
356  foreach ($productsSku as $productSku) {
357  $product = $this->productRepository->get($productSku);
358  $product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
359  $this->productRepository->save($product);
360  }
361 
362  $bundleProduct = $this->productRepository->get('bundle-product');
363 
364  // setting all options as not required
365  $options = $bundleProduct->getExtensionAttributes()->getBundleProductOptions();
366  foreach ($options as $productOption) {
367  $productOption->setRequired(false);
368  }
369 
370  $extension = $bundleProduct->getExtensionAttributes();
371  $extension->setBundleProductOptions($options);
372  $bundleProduct->setExtensionAttributes($extension);
373 
374  $bundleProduct = $this->productRepository->save($bundleProduct);
375 
376  $this->assertTrue(
377  $bundleProduct->isSalable(),
378  'Bundle product supposed to be saleable
379  if it has at least one not required option with saleable selection'
380  );
381  }
382 
383  private function setQtyForSelections($productsSku, $qty)
384  {
385  foreach ($productsSku as $productSku) {
386  $product = $this->productRepository->get($productSku);
387  $ea = $product->getExtensionAttributes();
388  $ea->getStockItem()->setQty($qty);
389  $this->productRepository->save($product);
390  }
391  }
392 }
$bundleProduct