diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 765c412..e998fe4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -49,17 +49,20 @@ jobs: strategy: fail-fast: false matrix: - php-version: ['8.2', '8.3'] + php-version: ['8.2', '8.3', '8.4'] steps: - uses: actions/checkout@v4 with: path: module + # Magento 2.4.7 does not run on PHP 8.4; that version gets the lint and isolated checks only. - uses: actions/checkout@v4 + if: matrix.php-version != '8.4' with: repository: magento/magento2 ref: 2.4.7-p10 path: magento - uses: actions/checkout@v4 + if: matrix.php-version != '8.4' with: repository: magento/inventory ref: 1.2.7-p10 @@ -77,12 +80,14 @@ jobs: find Api Block Console Controller Model Observer Plugin Setup Test view registration.php \( -name '*.php' -o -name '*.phtml' \) -print0 | xargs -0 -n1 php -l for test in Test/*.test.php; do php "$test"; done - name: Install Magento unit test dependencies + if: matrix.php-version != '8.4' run: | cp -R ../inventory/InventoryApi ../inventory/InventorySalesApi ../magento/app/code/Magento/ mkdir -p ../magento/app/code/Hellotext ln -s "$GITHUB_WORKSPACE/module" ../magento/app/code/Hellotext/Tracking composer --working-dir=../magento install --no-interaction --prefer-dist --no-progress - name: Run PHPUnit against Magento 2.4.7-p10 + if: matrix.php-version != '8.4' working-directory: magento run: vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Hellotext/Tracking/Test/Unit diff --git a/CHANGELOG.md b/CHANGELOG.md index 363f4eb..25289b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ # Changelog -## Unreleased +## 1.0.1 - 2026-10-19 + +- Install on stores without Magento Inventory (MSI): stock checks use Magento's default stock when MSI is not enabled. +- Allow the fonts of Hellotext popups, webchat and forms in the Content Security Policy. +- Allow PHP 8.4, used by Magento 2.4.8. + +## 1.0.0 - 2026-10-17 - Track page views, products, carts and checkout activity through Hellotext.js. - Display Hellotext popups, webchat, WhatsApp widgets and forms. diff --git a/Model/StockManagement.php b/Model/StockManagement.php index 2245401..dc0edc0 100644 --- a/Model/StockManagement.php +++ b/Model/StockManagement.php @@ -9,8 +9,12 @@ use Hellotext\Tracking\Model\Data\StockResult; use Magento\Catalog\Model\Product\Attribute\Source\Status; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; +use Magento\CatalogInventory\Api\StockRegistryInterface; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Framework\Module\Manager as ModuleManager; +use Magento\Framework\ObjectManagerInterface; use Magento\Framework\Webapi\Rest\Response; use Magento\InventorySalesApi\Api\AreProductsSalableInterface; use Magento\InventorySalesApi\Api\StockResolverInterface; @@ -19,13 +23,16 @@ class StockManagement implements StockManagementInterface { + // Magento Inventory (MSI) is optional: its services are resolved only when it is enabled, + // so stores that removed or disabled it can still install and compile the module. public function __construct( private CollectionFactory $collections, private ScopeConfigInterface $config, private StoreManagerInterface $stores, - private StockResolverInterface $stockResolver, - private AreProductsSalableInterface $salability, - private Response $response + private Response $response, + private ModuleManager $modules, + private ObjectManagerInterface $objects, + private StockRegistryInterface $legacyStock ) {} public function getStock(array $productIds): StockResultInterface @@ -66,11 +73,9 @@ public function getStock(array $productIds): StockResultInterface $salable = []; if ($skus !== []) { - $website = $this->stores->getWebsite((int) $store->getWebsiteId()); - $stock = $this->stockResolver->execute('website', (string) $website->getCode()); - foreach ($this->salability->execute(array_values($skus), (int) $stock->getStockId()) as $result) { - $salable[$result->getSku()] = $result->isSalable(); - } + $salable = $this->inventoryAvailable() + ? $this->inventorySalability(array_values($skus), (int) $store->getWebsiteId()) + : $this->legacySalability(array_values($skus)); foreach ($skus as $sku) { if (!array_key_exists($sku, $salable)) { throw new LocalizedException(__('Inventory did not return a result for every product.')); @@ -85,4 +90,42 @@ public function getStock(array $productIds): StockResultInterface ); return new StockResult($installation, $storeId, $business, $items); } + + // Removing the MSI packages can leave a stale enabled flag in app/etc/config.php, so its classes must exist too. + private function inventoryAvailable(): bool + { + return $this->modules->isEnabled('Magento_InventorySalesApi') + && interface_exists(StockResolverInterface::class) + && interface_exists(AreProductsSalableInterface::class); + } + + /** @return array */ + private function inventorySalability(array $skus, int $websiteId): array + { + $website = $this->stores->getWebsite($websiteId); + $stock = $this->objects->get(StockResolverInterface::class)->execute('website', (string) $website->getCode()); + $salable = []; + foreach ($this->objects->get(AreProductsSalableInterface::class)->execute($skus, (int) $stock->getStockId()) as $result) { + $salable[$result->getSku()] = $result->isSalable(); + } + return $salable; + } + + /** + * Without MSI, Magento keeps one global stock whose status already accounts for quantity and backorders. + * + * @return array + */ + private function legacySalability(array $skus): array + { + $salable = []; + foreach ($skus as $sku) { + try { + $salable[$sku] = (int) $this->legacyStock->getProductStockStatusBySku($sku) === 1; + } catch (NoSuchEntityException) { + $salable[$sku] = false; + } + } + return $salable; + } } diff --git a/README.md b/README.md index 1ffcede..6d60ad4 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,9 @@ Connect your Magento store to Hellotext to track storefront activity and orders, ## Requirements -- Magento Open Source 2.4.7 -- PHP 8.2 or 8.3 +- Magento Open Source 2.4 (tested on 2.4.7) +- PHP 8.2, 8.3 or 8.4 +- Magento Inventory (MSI) is optional; without it, stock checks use Magento's default stock - An HTTPS storefront and a Magento API reachable by Hellotext ## Install diff --git a/Test/README.md b/Test/README.md index c13d1a8..1212429 100644 --- a/Test/README.md +++ b/Test/README.md @@ -1,6 +1,6 @@ # Running tests -Use Node.js 22 and PHP 8.2 or 8.3. From the module directory: +Use Node.js 22 and PHP 8.2, 8.3 or 8.4. From the module directory: ```sh composer validate diff --git a/Test/Unit/Model/StockManagementTest.php b/Test/Unit/Model/StockManagementTest.php index 4c9fc5a..86462d2 100644 --- a/Test/Unit/Model/StockManagementTest.php +++ b/Test/Unit/Model/StockManagementTest.php @@ -8,8 +8,12 @@ use Magento\Catalog\Model\Product\Attribute\Source\Status; use Magento\Catalog\Model\ResourceModel\Product\Collection; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; +use Magento\CatalogInventory\Api\StockRegistryInterface; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Framework\Module\Manager as ModuleManager; +use Magento\Framework\ObjectManagerInterface; use Magento\Framework\Webapi\Rest\Response; use Magento\InventoryApi\Api\Data\StockInterface; use Magento\InventorySalesApi\Api\AreProductsSalableInterface; @@ -29,6 +33,8 @@ class StockManagementTest extends TestCase private Collection $collection; private AreProductsSalableInterface $salability; private StockResolverInterface $resolver; + private StockRegistryInterface $legacyStock; + private bool $inventoryEnabled = true; protected function setUp(): void { @@ -61,7 +67,35 @@ protected function setUp(): void $response->expects(self::once())->method('setHeader')->with( 'Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store', true ); - $this->subject = new StockManagement($collections, $config, $stores, $this->resolver, $this->salability, $response); + $modules = $this->createMock(ModuleManager::class); + $modules->method('isEnabled')->with('Magento_InventorySalesApi')->willReturnCallback(fn (): bool => $this->inventoryEnabled); + $objects = $this->createMock(ObjectManagerInterface::class); + $objects->method('get')->willReturnMap([ + [StockResolverInterface::class, $this->resolver], + [AreProductsSalableInterface::class, $this->salability], + ]); + $this->legacyStock = $this->createMock(StockRegistryInterface::class); + $this->subject = new StockManagement($collections, $config, $stores, $response, $modules, $objects, $this->legacyStock); + } + + public function testFallsBackToTheGlobalStockWhenInventoryIsDisabled(): void + { + $this->inventoryEnabled = false; + $this->collection->method('getItems')->willReturn([ + $this->product(11, 'SIMPLE'), $this->product(12, 'GONE'), $this->product(13, 'SOLD-OUT') + ]); + $this->legacyStock->method('getProductStockStatusBySku')->willReturnCallback(static function (string $sku): int { + if ($sku === 'GONE') { + throw new NoSuchEntityException(__('Missing')); + } + return $sku === 'SIMPLE' ? 1 : 0; + }); + $this->resolver->expects(self::never())->method('execute'); + $this->salability->expects(self::never())->method('execute'); + + $result = $this->subject->getStock([11, 12, 13]); + + self::assertSame([true, false, false], array_map(static fn ($item) => $item->getSalable(), $result->getItems())); } public function testChecksSimpleProductsParentsAndExactChildrenTogetherInTheWebsiteStock(): void diff --git a/composer.json b/composer.json index 20b4bed..afcd7bc 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "hellotext/magento", "description": "Connect Magento Open Source stores to Hellotext: storefront activity, customer identity, orders and catalog/customer import.", "type": "magento2-module", - "version": "1.0.0", + "version": "1.0.1", "license": "proprietary", "keywords": [ "magento", @@ -20,10 +20,11 @@ "docs": "https://github.com/hellotext/magento/blob/main/docs/getting-started.md" }, "require": { - "php": ">=8.2 <8.4", + "php": ">=8.2 <8.5", "magento/framework": "^103.0", "magento/module-backend": "^102.0", "magento/module-catalog": "^104.0", + "magento/module-catalog-inventory": "^100.4", "magento/module-checkout": "^100.4", "magento/module-config": "^101.2", "magento/module-configurable-product": "^100.4", @@ -31,13 +32,15 @@ "magento/module-csp": "^100.4", "magento/module-customer": "^103.0", "magento/module-integration": "^100.4", - "magento/module-inventory-sales-api": "^1.2", "magento/module-newsletter": "^100.4", "magento/module-quote": "^101.2", "magento/module-sales": "^103.0", "magento/module-store": "^101.1", "magento/module-webapi": "^100.4" }, + "suggest": { + "magento/module-inventory-sales-api": "Checks product availability in each website's stock when Magento Inventory (MSI) is installed." + }, "autoload": { "files": [ "registration.php" diff --git a/etc/csp_whitelist.xml b/etc/csp_whitelist.xml index 2eb2c77..74f611f 100644 --- a/etc/csp_whitelist.xml +++ b/etc/csp_whitelist.xml @@ -17,6 +17,11 @@ https://www.hellotext.com + + + https://www.hellotext.com + + https://api.hellotext.com diff --git a/etc/module.xml b/etc/module.xml index c194dd6..e1f2fc3 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -4,6 +4,7 @@ + @@ -11,7 +12,6 @@ -