Magento 2 GraphQL Shipping Tests: Navigating Sample Data Configuration Conflicts

For Magento 2 developers and quality assurance teams, ensuring the reliability of automated tests is paramount. However, a common challenge arises when integrating sample data into development and testing environments: unexpected configuration clashes. This insight delves into a specific issue where Magento 2's GraphQL API functional tests for shipping methods encounter failures when the robust sample data is installed, primarily due to the default setup of the Table Rate shipping method.

The core of the problem lies in how Magento's sample data configures the Table Rate shipping method. Upon installation, sample data globally enables Table Rate and sets its condition to package_value_with_discount. This default configuration directly conflicts with the expectations of certain GraphQL API functional tests:

  • Magento\GraphQl\Quote\Guest\CheckoutEndToEndTest::testCheckoutWorkflow: This test expects a single shipping method to be available. However, with sample data installed, Table Rate is added as a second method, causing the test to fail.

  • Magento\GraphQl\Quote\Guest\SetOfflineShippingMethodsOnCartTest::testSetOfflineShippingMethod: This test is designed to load weight-based Table Rate rows. Yet, the sample data's default condition (package_value_with_discount) prevents these rows from being correctly evaluated based on package weight, leading to a test failure.

These discrepancies can lead to frustrating false negatives in CI/CD pipelines and hinder consistent development practices, especially when different environments (with and without sample data) yield varying test results.

The proposed solution, as outlined in the associated pull request, addresses these conflicts by introducing method-scoped configuration fixtures. These fixtures are designed to modify the Table Rate settings specifically for the duration of the tests, ensuring a controlled environment:

  • For the checkout scenario, the fixtures disable Table Rate, allowing testCheckoutWorkflow to find the expected single shipping method.

  • For the offline shipping scenario, the fixtures temporarily set the Table Rate condition to package_weight, enabling testSetOfflineShippingMethod to correctly evaluate weight-based rates.

It's crucial to note that these changes are confined to the test setup and do not alter the core application's shipping configuration, maintaining the integrity of the production environment.

Developers can replicate and verify this fix using fresh Magento 2.4-develop installs, both with and without sample data, leveraging tools like Warden PHP-FPM. The detailed steps involve setting up the environment, linking sample data (if applicable), installing Magento, and then running the targeted GraphQL API functional tests. The goal is to observe test failures on the sample-data baseline before the fix and then confirm successful passes with the applied fixtures.

Below are the manual testing scenarios and commands provided in the issue, which are invaluable for any developer looking to understand or implement this fix:

Warden PHP-FPM setup and targeted commands
# Sample-data variant only
git clone --branch 2.4-develop --single-branch \
  https://github.com/magento/magento2-sample-data.git ../magento2-sample-data

warden svc up
warden env up -d --wait
warden env exec -T php-fpm composer install

# Sample-data variant only; link before setup:install
warden env exec -T php-fpm php -f /var/www/sample-data/dev/tools/build-sample-data.php -- \
  --command=link \
  --ce-source=/var/www/html \
  --sample-data-source=/var/www/sample-data

Install a fresh store. Set APP_URL to the Warden URL and the two password variables to disposable local credentials. For the no-sample-data variant, skip the clone, mount, linker, and later setup:upgrade command.

APP_URL="https://app.example.test/"
LOCAL_DB_PASSWORD=""
LOCAL_ADMIN_PASSWORD=""

warden env exec -T php-fpm php -d memory_limit=-1 bin/magento setup:install \
  --cleanup-database \
  --base-url="${APP_URL}" \
  --base-url-secure="${APP_URL}" \
  --use-secure=1 \
  --use-secure-admin=1 \
  --use-rewrites=1 \
  --backend-fr \
  --db-host=db \
  --db-name=magento \
  --db-user=root \
  --db-password="${LOCAL_DB_PASSWORD}" \
  --admin-firstname=Admin \
  --admin-lastname=Admin \
  [email protected] \
  --admin-user=admin \
  --admin-password="${LOCAL_ADMIN_PASSWORD}" \
  --language=en_US \
  --currency=USD \
  --timez \
  --search-engine=opensearch \
  --opensearch-host=opensearch \
  --opensearch-port=9200 \
  --opensearch-enable-auth=0 \
  --opensearch-timeout=15 \
  --session-save=db \
  -vvv

# Sample-data variant only
warden env exec -T php-fpm php -d memory_limit=-1 bin/magento setup:upgrade

# The API-functional bootstrap applies these when it performs installation.
# For a preinstalled store, run them once before the targeted tests.
warden env exec -T php-fpm bin/magento indexer:set-mode realtime -vvv
warden env exec -T php-fpm bin/magento indexer:reindex -vvv

cp dev/tests/api-functional/phpunit_graphql.xml.dist dev/tests/api-functional/phpunit_graphql.local.xml
sed -i "s|value="http://magento.url"|value="${APP_URL}"|" dev/tests/api-functional/phpunit_graphql.local.xml

warden env exec -T -w /var/www/html/dev/tests/api-functional php-fpm \
  ../../../vendor/bin/phpunit --colors=never -c ./phpunit_graphql.local.xml \
  --filter CheckoutEndToEndTest::testCheckoutWorkflow \
  /var/www/html/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CheckoutEndToEndTest.php

warden env exec -T -w /var/www/html/dev/tests/api-functional php-fpm \
  ../../../vendor/bin/phpunit --colors=never -c ./phpunit_graphql.local.xml \
  --filter SetOfflineShippingMethodsOnCartTest::testSetOfflineShippingMethod \
  /var/www/html/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetOfflineShippingMethodsOnCartTest.php

This fix underscores the importance of meticulous test environment management in complex platforms like Magento 2. By ensuring that automated tests are robust and independent of specific sample data configurations, developers can maintain higher code quality and streamline their development workflows, ultimately leading to a more stable and predictable e-commerce platform.

Start with the tools

Explore migration tools

See options, compare methods, and pick the path that fits your store.

Explore migration tools