# Hyvä Checkout Playwright Tests

## To install playwright and run the tests:

1. `cd src/Test/Playwright`
2. `npm ci`
3. `npx playwright install --with-deps`
4. `echo "PLAYWRIGHT_BASE_URL=https://my.local-dev-instance.test/" > .env`
5. `npx playwright test`

More information can be found in the docs: <https://playwright.dev/docs/intro>

## Preparing the system

The tests expect **Sample Data** to be installed, **Hyvä Theme** and **Hyvä Checkout** to be active and the **Purchase Order** payment method to be enabled.  

(The following instructions assume [n98-magerun2](https://github.com/netz98/n98-magerun2#installation) is installed as `magerun2`)

1. If it isn't present already, install the Hyvä Theme and Hyvä Checkout:  
   `composer require hyva-themes/magento2-default-theme hyva-themes/magento2-hyva-checkout`
2. Install Magento sample data:  
   `bin/magento sample-data:deploy`
3. Run the migrations:  
   `bin/magento setup:upgrade`
4. Set the default theme to Hyvä, either manually in the admin or, in CI, this does the job:

   ```bash
    THEME_ID=$(mysql -u"$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE" --silent --skip-column-names -e "SELECT theme_id FROM theme WHERE code = 'Hyva/default'")
    magerun2 config:store:set --scope=stores --scope-id=1 design/theme/theme_id $THEME_ID
   ```
   (The theme is configured using `magerun2` because `bin/magento config:set` complains about invalid path `design/theme/theme_id`)
5. Enable Hyvä checkout, either in the admin or using `magerun2`:  
   `magerun2 config:store:set --scope=stores --scope-id=1 hyva_themes_checkout/general/checkout default`
6. Disable the legacy Magento Captcha, either in the admin or in the terminal:  
   `bin/magento config:set --lock-env customer/captcha/enable 0`
7. Enable the built in Purchase Order payment method, either in the admin or in the terminal:  
   `bin/magento config:set --lock-env payment/purchaseorder/active 1`
8. Enable signing in from within the checkout, which ships turned off:  
   `bin/magento config:set --lock-env hyva_themes_checkout/component/guest_details/enable_login 1`  
   (Without it the guest details login test skips itself.)
9. Compile Hyvä CSS:

   ```bash
   npm --prefix vendor/hyva-themes/magento2-default-theme/web/tailwind ci
   npm --prefix vendor/hyva-themes/magento2-default-theme/web/tailwind run build-prod
   ```
10. Enable developer mode:  
   `bin/magento deploy:mode:set developer`
11. Flush the cache:  
    `bin/magento cache:flush`

## Feature flag tests

`hyva/checkout-types/` holds the tests that run against whichever checkout type is configured. `hyva/features/`
holds tests that assert a specific system configuration value, so they can only pass when the store is
configured to match. Each of those specs skips itself unless an environment variable says the store is in the
state it covers.

### Street address lines

`hyva/features/street-lines/` covers `customer/address/street_lines = 1`. Only the single line state has a
spec; the default of two lines is already exercised by the whole `hyva/checkout-types/` suite.

```bash
bin/magento config:set customer/address/street_lines 1
bin/magento cache:flush
STREET_LINES=1 npx playwright test hyva/features/street-lines/single-line.spec.js
```

Use `bin/magento config:set` here rather than `magerun2 config:store:set`: only the former runs the config
field's backend model, which is what mirrors the value into the street attribute's `multiline_count` — where
`Customer\Helper\Address::getStreetLines()` actually reads it from.

Remember to set the value back to `2` afterwards — the rest of the suite assumes the default, and would
otherwise run against single line address forms.

### Postcode format validation

`hyva/features/postcode-validation/` covers `hyva_themes_checkout/address_form/postcode/enable_format_validation`
in both states. The disabled spec runs by default, because the flag ships switched off.

```bash
# Default state — verifies no format rule is attached and the requiredness behaviour is unaffected.
npx playwright test hyva/features/postcode-validation/format-validation-disabled.spec.js

# Enabled state — flip the config first, otherwise the spec skips.
magerun2 config:store:set --scope=stores --scope-id=1 hyva_themes_checkout/address_form/postcode/enable_format_validation 1
bin/magento cache:flush
POSTCODE_FORMAT_VALIDATION=1 npx playwright test hyva/features/postcode-validation/format-validation-enabled.spec.js
```

Remember to set the value back to `0` afterwards — the rest of the suite assumes the default. In particular,
address fixtures must use postcodes that are valid for their country while the flag is on.
