# Testing

## Automated (what was actually run)
Environment: Ubuntu, PHP 8.3.6, MariaDB 10.11, Apache 2.4.58 (mod_php, sub-path `/bizmtaani/`) and PHP built-in server.

| Suite | Command | Result |
|---|---|---|
| Phase 3 (search, filters, distance vs independent haversine, open-now edges, injection/wildcard safety, geocoding with caching/rate-limit/failure, privacy, pages) | `php tests/phase3.php` | 107 passed, 0 failed (Apache sub-path and dev server) |
| Map JavaScript in jsdom with real Leaflet (geolocation success/denied/error/outside Kenya/unsupported, markers, XSS-safe popups, picker) | `node tests/js_test.js` (needs `npm i jsdom@24`) | 14 passed, 1 skipped |
| Phase 2 end-to-end (provider lifecycle, uploads, IDOR, documents, verification decisions and emails, public visibility, categories/synonyms, retention) | `php tests/phase2.php` | 123 passed, 0 failed (Apache sub-path and dev server) |
| End-to-end over HTTP (guests, CSRF, registration, emails, verification, roles, login lockout, password reset, account, admin, suspension, employees/permissions, settings, unit checks, dedupe) | `php tests/e2e.php` | 110 passed, 0 failed (Apache and dev server) |
| PHPMailer via SMTP against a fake SMTP server (accepted, wiped payload, retry/backoff, permanent failure, password scrubbing, production guard, bad address) | `php tests/smtp_test.php` | 14 passed, 0 failed |
| QR output decodes back to exact payload (OpenCV) | manual script | passed |
| `.htaccess` blocks `.env`, `app/`, `lib/`, `database/`, `storage/`, `config/`, `*.md` | curl on Apache | 403 on all |
| Migration idempotent | `php bin/migrate.php` twice | second run skips |

Run order for the SMTP suite: it expects the fake server from `tests/fake_smtp.py` on ports 2525 and 2526 (`python3 tests/fake_smtp.py 2525 ok` and `... 2526 rejectauth`).

## NOT tested
- Real Windows/XAMPP, other PHP versions (8.1, 8.2), MySQL 8 proper
- A real SMTP provider (TLS/STARTTLS paths)
- Session idle/absolute timeout expiry (logic written, not exercised)
- Browser rendering on phones, accessibility, load/performance
- Cron on a real scheduler (script runs; scheduling not tested)

## Phase 2 notes
- Uploads folder rule verified on Apache: `evil.php`, `evil.php.jpg`, a `.php` with a 24-hex name and `.html` all return 403.
- PDFs are validated by real MIME/signature only (not malware-scanned). They are stored under a random `.bin` name and served as `attachment` with `nosniff`.
- Staff opening a document is written to the audit log; the owner opening their own is not.
- Not tested: Windows paths/permissions for `public/uploads` and `storage/private_uploads`, very large real-world phone photos on slow networks, WebP on hosts without GD WebP support.

## Phase 3 notes
- Geocoding tests use `tests/fake_geocoder.py 8765`, never the real OpenStreetMap service. Real Nominatim has NOT been exercised.
- Performance (5,035 businesses / 10,029 services, MariaDB 10.11, best of 3): category+county 4 ms, place text 5 ms, radius 5 km 11 ms, keyword+radius 20 ms, radius+price+open-now 56 ms, synonym keyword 65 ms, free-text keyword matching every row 130-165 ms. MariaDB chose a table scan rather than the geo index at this size. LIKE search is fine into the low thousands; plan FULLTEXT or a search engine before tens of thousands.
- Not tested: the approximate-area circle (jsdom cannot draw vector layers), tile loading, drag behaviour and the real browser permission prompt, Windows/XAMPP.
- Words shorter than 2 characters and common filler words ("in", "near", "wa", "ya") are ignored in keyword search.


## Phase 4 manual acceptance (Windows XAMPP)

Use a disposable/local database and two separate browser profiles (one customer, one provider). Ensure both accounts are active, the customer's email is verified, the provider's business is approved and published, and at least one fixed-price service is published.

1. Run `C:\xampp\php\php.exe bin\migrate.php`; confirm `004_bookings.sql` is applied once and a second run skips it.
2. Log in as the customer, open the provider's public business page and submit a future booking. Confirm the customer is redirected to booking details and sees a unique `BM-...` reference.
3. Confirm `bookings`, `booking_items` and `booking_status_history` contain the request, the correct server-side price snapshot and the initial status.
4. Submit the same POST again with the same idempotency key. Confirm no second booking is created and no duplicate booking email is queued.
5. Log in as the owning provider and open **Bookings**. Confirm the request appears. A different provider must not see it or change its status.
6. Confirm the request. Confirm the customer sees the updated status and a QR image when using the same customer session. A QR token hash, not the raw token, must be stored in `qr_tokens`.
7. Scan the displayed QR with a phone camera while logged in as the owning provider, do not use a direct manual check-in action; the provider status endpoint must reject attempts to bypass QR verification. Confirm the booking changes to `checked_in` and one `checked_in` event is inserted.
8. Scan again. Confirm no second check-in event is created and the status remains unchanged. A different provider must receive an access-denied response.
9. Test invalid transitions (for example, completing a pending booking), invalid quantities, past dates, an unpublished service, a price-on-request service and duplicate appointment times. Each must be rejected safely.
10. Test customer cancellation before and inside the configured cutoff window.
11. With `MAIL_DRIVER=log`, inspect `storage/logs/mail/` for booking-created and status-update messages. With SMTP configured, confirm the outbox marks accepted only when the SMTP server accepts the message.
12. Check `admin/bookings` using an admin account or employee with `bookings.view`. The recorded total is booking value, not money received.

The current development sandbox did not have the `mbstring` PHP extension or a MySQL/MariaDB server, so the end-to-end database and SMTP suites could not be executed here. PHP syntax lint was run against all PHP files. Complete the manual acceptance checks on the target XAMPP environment before using real customer data.


## Phase 4 revised manual tests
1. In Provider → Products, create a fixed-price item with a photo and tracked stock; confirm it appears on the public business page.
2. Create a product with seller-confirmed pricing and add it to a customer cart.
3. Submit the order and check My Orders, the seller order queue, and the customer's receipt email. The email should show line items and a real embedded QR image.
4. Accept the order and enter a price for each unpriced item. Confirm the total updates and the customer gets a status email.
5. Move the order to Ready for collection; scan its QR while signed in as the owning provider. Confirm it becomes Collected, and a repeat scan is rejected. Test delivery orders separately.
6. Create a service booking; have the provider confirm it. Confirm the customer receives a booking receipt email with the QR image and booking details.
7. Temporarily break SMTP settings and verify order/booking actions remain saved while email retries are recorded.

This environment's PHP CLI may lack `pdo_mysql`, `mbstring`, and `gd`; enable these extensions in the XAMPP `php.ini` used by Apache and CLI before testing. QR generation requires GD; the database app requires PDO MySQL; text validation uses mbstring.

## Customer dashboard and QR persistence checks
- Log in as a customer and confirm the dashboard shows Explore Marketplace, recent products/services, My Orders, My Bookings and Cart.
- Place an order, open its detail page and confirm the QR image appears on the page as well as in the email.
- Log out and log back in (or use another browser session) and reopen the same order; the QR should still appear.
- Confirm a booking, open its booking detail page and confirm its QR remains available after a fresh login.
- Open a business page as a guest, click Log in to order/book, sign in and verify you return to that business page.


## Launch-readiness update checks (migration 008)

1. Apply `008_launch_workflow.sql` on a disposable database and confirm `bookings.started_at` exists.
2. Confirm a booking as its owning provider; verify its customer QR is visible.
3. Attempt to POST `status=checked_in` to the provider status endpoint without scanning. It must be rejected.
4. Scan the valid booking QR while signed in as its owning provider. Confirm status becomes `checked_in` and the matching `booking_events` row has `event_type=checked_in` and `event_data=qr`.
5. Attempt to complete directly from `checked_in`; it must be rejected. Start the service first; confirm `started_at` and a `started` event are recorded. Then complete.
6. Open Admin → Bookings & Orders. Test single-day, full-month and multi-month date ranges; confirm start and end dates are inclusive. Test service/product/all, status, provider and customer filters independently. Compare dashboard totals with direct SQL counts.
7. Confirm product orders are reported separately from service bookings and that their completion metric counts collected/delivered orders only.

These checks are not represented as passed merely because PHP syntax validation succeeded. The current build environment did not provide a configured project database for full integration testing.
