Skip to content

Commit

Permalink
Merge pull request #109 from waleedbasit/add-shipment-with-0-qty
Browse files Browse the repository at this point in the history
Adds plugin to ensure shipment can be sent with qty is 0, solves issue #69 and #108
  • Loading branch information
convenient authored Apr 25, 2023
2 parents ee3e9ca + 5c99009 commit 66aedb3
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ install:
- composer install --no-interaction
- PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --dry-run --rules=@PSR2 --diff src/
- vendor/bin/phpcs -s --standard=./ruleset.xml src/
- CURRENT_EXTENSION="." FULL_INSTALL=1 vendor/bin/mtest-make $TEST_GROUP
- TEST_GROUP=$TEST_GROUP composer docker-install-magento

script:
- composer docker-configure-magento
- composer docker-run-codeception
- composer docker-run-unit-tests
- composer docker-run-integration-tests

cache:
directories:
Expand Down
15 changes: 13 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"config": {
"allow-plugins": {
"magento/*": true
}
},
"process-timeout": 1200
},
"repositories": [
{
Expand All @@ -33,9 +34,13 @@
"ampersand/magento-docker-test-instance": "^0.1",
"codeception/module-rest": "^1.2.0",
"friendsofphp/php-cs-fixer": "^2.16",
"magento/magento-coding-standard": "<16"
"magento/magento-coding-standard": "<16",
"tddwizard/magento2-fixtures": "^1.1"
},
"scripts": {
"docker-install-magento": [
"CURRENT_EXTENSION=\".\" FULL_INSTALL=1 COMPOSER_REQUIRE_EXTRA='tddwizard/magento2-fixtures' TWOFACTOR_ENABLED=1 UNIT_TESTS_PATH='dev/MagentoTests/Unit' INTEGRATION_TESTS_PATH='dev/MagentoTests/Integration' vendor/bin/mtest-make $TEST_GROUP"
],
"docker-configure-magento": [
"vendor/bin/mtest 'vendor/bin/n98-magerun2 config:store:set checkout/options/guest_checkout 1'",
"vendor/bin/mtest 'vendor/bin/n98-magerun2 config:store:set payment/checkmo/active 1'",
Expand All @@ -47,6 +52,12 @@
"docker-run-codeception": [
"URL=\"http://0.0.0.0:1234/\" MYSQL_USER=\"root\" MYSQL_HOST=\"0.0.0.0\" MYSQL_DB=\"magento\" MYSQL_PORT=\"1235\" ./dev/run-codeception.sh"
],
"docker-run-unit-tests": [
"vendor/bin/mtest 'vendor/bin/phpunit -c /var/www/html/dev/tests/unit/phpunit.xml.dist --testsuite Unit --debug'"
],
"docker-run-integration-tests": [
"vendor/bin/mtest 'vendor/bin/phpunit -c /var/www/html/dev/tests/integration/phpunit.xml.dist --testsuite Integration --debug'"
],
"post-install-cmd": [
"([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/)"
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php
declare(strict_types=1);
namespace Ampersand\DisableStockReservation\Test\Integration\InventoryInStorePickupSalesAdminUi;

use Magento\TestFramework\Helper\Bootstrap;
use TddWizard\Fixtures\Catalog\ProductBuilder;
use TddWizard\Fixtures\Catalog\ProductFixture;
use TddWizard\Fixtures\Checkout\CustomerCheckout;
use TddWizard\Fixtures\Checkout\CartBuilder;
use TddWizard\Fixtures\Customer\CustomerFixture;
use TddWizard\Fixtures\Customer\CustomerBuilder;
use TddWizard\Fixtures\Customer\AddressBuilder;

/**
* @magentoAppArea adminhtml
*/
class NotifyPickupControllerTest extends \Magento\TestFramework\TestCase\AbstractBackendController
{
/** @var ProductFixture */
private $productFixture;

/** @var CustomerFixture */
private $customerFixture;

/**
* Because stock reservation on order placement is disabled, we are trying to notify on products which may either
* have some stock, or may be all the way at 0 stock.
*
* The admin user clicks this button to notify the customer it's ready for pickup, but without the changes in
* IsFulfillablePlugin we cant actually let them know when the stock it at 0 because it fails the isItemFulfillable
* check
*
* @link https://github.com/magento/inventory/blob/c4e3a4ef/InventoryInStorePickupSalesAdminUi/Controller/Adminhtml/Order/NotifyPickup.php#L73-L95
* @link https://github.com/magento/inventory/blob/c4e3a4ef/InventoryInStorePickupSales/Model/Order/IsFulfillable.php#L68-L84
* @link https://github.com/AmpersandHQ/magento2-disable-stock-reservation/pull/109
* @link https://github.com/AmpersandHQ/magento2-disable-stock-reservation/issues/69
*
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function testNotifyProducts()
{
if (!class_exists(\Magento\InventoryInStorePickupSales\Model\Order\IsFulfillable::class)) {
$this->markTestSkipped('Test not required on older magento versions');
return;
}

/**
* Create a customer and login
*/
$this->customerFixture = new CustomerFixture(CustomerBuilder::aCustomer()->withAddresses(
AddressBuilder::anAddress()->asDefaultBilling(),
AddressBuilder::anAddress()->asDefaultShipping()
)->build());
$this->customerFixture->login();

/**
* Create a product with 5 qty
*/
$this->productFixture = new ProductFixture(
ProductBuilder::aSimpleProduct()
->withPrice(10)
->withStockQty(5)
->withIsInStock(true)
->build()
);

/**
* Order 5 qty
*/
$checkout = CustomerCheckout::fromCart(
CartBuilder::forCurrentSession()
->withSimpleProduct(
$this->productFixture->getSku(),
5
)
->build()
);
$order = $checkout->placeOrder();
$extensionAttributes = $order->getExtensionAttributes();
$extensionAttributes->setPickupLocationCode('abc123');
$order->setExtensionAttributes($extensionAttributes);
$order->save();

$this->assertGreaterThan(0, strlen($order->getIncrementId()), 'the order does not have a valid increment_id');
$this->assertIsNumeric($order->getId(), 'the order does not have an entity_id');

/**
* Load the product fresh, and confirm it has had stock decremented (because reservations are disabled)
*/
$product = Bootstrap::getObjectManager()
->get(\Magento\Catalog\Model\ProductFactory::class)
->create()
->loadByAttribute('sku', $this->productFixture->getSku());
$this->assertEquals($product->getId(), $this->productFixture->getId(), 'The product failed to load');
$this->assertEquals(0, $product->getQty(), 'The product should have qty=0 after the order');
$this->assertEquals(0, $product->getIsInStock(), 'The product should have is_in_stock=0 after the order');

/**
* Dispatch the notfiyPickup controller
*
* It should respond that the customer has been notified
*
* The plugin will force every case to be a "true" result for pickup being available, the only thing that
* disqualifies a product from being able to be picked up in the original implementation is it being OOS
*
* @link https://github.com/magento/inventory/blob/develop/InventoryInStorePickupSalesAdminUi/Controller/Adminhtml/Order/NotifyPickup.php
*/
$this->getRequest()->setParam(
'order_id',
$order->getId()
);
$this->dispatch('backend/sales/order/notifyPickup');
$this->assertEquals(1, count($this->getSessionMessages()), 'We should only have 1 session message');
$this->assertSessionMessages(
$this->equalTo(
['The customer has been notified and shipment created.'],
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
)
);
}

/**
* @return void
*/
protected function tearDown(): void
{
if (isset($this->productFixture)) {
$this->productFixture->rollback();
}
if (isset($this->customerFixture)) {
$this->customerFixture->rollback();;
}
}
}
18 changes: 18 additions & 0 deletions dev/MagentoTests/Unit/ExampleUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Ampersand\DisableStockReservation\Test\Unit;

use PHPUnit\Framework\TestCase;

class ExampleUnitTest extends TestCase
{
/**
* Example unit test to prove setup works, in case any future code wants to be unit tested
*
* @return void
*/
public function testExample()
{
$this->assertEquals('testExample', 'testExample');
}
}
6 changes: 4 additions & 2 deletions dev/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cd magento2-disable-stock-reservation

Boot the docker test instance for testing against (run `./vendor/bin/mtest-make` to see supported versions)
```
CURRENT_EXTENSION="." FULL_INSTALL=1 vendor/bin/mtest-make 2-4-5
TEST_GROUP=2-4-5 composer docker-install-magento
```

Configure magento test settings
Expand All @@ -18,6 +18,8 @@ composer docker-configure-magento

Run the tests
```
composer docker-run-codeception
composer docker-run-unit-tests # unit tests
composer docker-run-integration-tests # integration tests
composer docker-run-codeception # codeception rest api tests
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);

namespace Ampersand\DisableStockReservation\Plugin\InventoryInStorePickupSales\Model\Order;

use Magento\InventoryInStorePickupSales\Model\Order\IsFulfillable;

class IsFulfillablePlugin
{
/**
* @param IsFulfillable $subject
* @param bool $result
* @return bool
*/
public function afterExecute(IsFulfillable $subject, bool $result): bool
{
return true;
}
}
4 changes: 4 additions & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@

<preference for="Ampersand\DisableStockReservation\Api\SourcesRepositoryInterface"
type="Ampersand\DisableStockReservation\Model\SourcesRepository"/>
<!-- Allow sending pickup notif at 0 qty/out of stock -->
<type name="Magento\InventoryInStorePickupSales\Model\Order\IsFulfillable">
<plugin name="allow_pickup_notif" type="Ampersand\DisableStockReservation\Plugin\InventoryInStorePickupSales\Model\Order\IsFulfillablePlugin"/>
</type>
</config>

0 comments on commit 66aedb3

Please sign in to comment.