upskill-event-manager/wordpress-dev/tests/unit/test-registration-validation.php
bengizmo 37f7b426b6 feat: Implement auto page creation & fix login E2E tests
Implements automatic creation of required plugin pages (Community Login,
Trainer Registration, Trainer Dashboard) upon plugin activation. This
addresses E2E test failures caused by missing pages in the test
environment.

- Adds activation hook in `hvac-community-events.php` to call
  `hvac_ce_create_required_pages`.
- The callback function checks for existing pages by slug and creates
  them using `wp_insert_post` if missing. Includes debug logging.

Also fixes issues identified during E2E test debugging:
- Corrects fatal error in `includes/community/class-login-handler.php`
  by replacing undefined constant `HVAC_COMMUNITY_EVENTS_PATH` with
  `HVAC_CE_PLUGIN_DIR`.
- Updates `tests/e2e/tests/login.spec.ts` to use the correct selector
  `#wp-submit` for the login form submit button instead of
  `button[type="submit"]`.

Documentation updates:
- Adds `docs/automatic-page-creation-plan.md`.
- Updates `README.md` regarding automatic page creation.
- Updates Memory Bank files (`decisionLog.md`, `progress.md`,
  `activeContext.md`).

Note: Activation hook logging did not appear during WP-CLI activation,
requiring further investigation if page creation issues persist. E2E
test confirmation pending.
2025-03-28 17:18:21 -03:00

111 lines
No EOL
4.1 KiB
PHP

<?php
/**
* Unit tests for HVAC registration form validation
*/
class RegistrationValidationTest extends WP_UnitTestCase {
private $registration;
public function setUp(): void { // Added :void return type hint
parent::setUp();
require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-registration.php';
$this->registration = new HVAC_Registration();
}
public function test_required_fields_validation() {
$data = [
'first_name' => '',
'last_name' => '',
'business_name' => '',
'business_phone' => '',
'business_email' => '',
'user_country' => '',
'user_state' => '',
'user_city' => '',
'user_zip' => '',
'business_type' => '',
'user_pass' => 'ValidPass1'
];
$errors = $this->registration->validate_registration($data);
$required_fields = [
'first_name', 'last_name', 'business_name',
'business_phone', 'business_email', 'user_country',
'user_state', 'user_city', 'user_zip', 'business_type'
];
foreach ($required_fields as $field) {
$this->assertArrayHasKey($field, $errors);
// Email validation takes precedence over required check when empty
if ($field === 'business_email') {
$this->assertEquals('Please enter a valid email address', $errors[$field]);
} else {
$this->assertEquals('This field is required', $errors[$field]);
}
}
}
public function test_email_validation() { // Added missing method definition
$data = $this->get_valid_test_data();
$data['business_email'] = 'invalid-email';
$errors = $this->registration->validate_registration($data);
$this->assertArrayHasKey('business_email', $errors);
$this->assertEquals('Please enter a valid email address', $errors['business_email']);
}
public function test_password_validation() {
$test_cases = [
'short' => ['pass', 'Password must be at least 8 characters with uppercase, lowercase and numbers'],
'no_uppercase' => ['password1', 'Password must be at least 8 characters with uppercase, lowercase and numbers'],
'no_lowercase' => ['PASSWORD1', 'Password must be at least 8 characters with uppercase, lowercase and numbers'],
'no_number' => ['Password', 'Password must be at least 8 characters with uppercase, lowercase and numbers'],
'valid' => ['ValidPass1', null]
];
foreach ($test_cases as $case => $values) {
$data = $this->get_valid_test_data();
$data['user_pass'] = $values[0];
$errors = $this->registration->validate_registration($data);
if ($values[1] === null) {
$this->assertArrayNotHasKey('user_pass', $errors);
} else {
$this->assertArrayHasKey('user_pass', $errors);
$this->assertEquals($values[1], $errors['user_pass']);
}
}
}
public function test_url_validation() {
$data = $this->get_valid_test_data();
$data['business_website'] = 'invalid-url';
$errors = $this->registration->validate_registration($data);
$this->assertArrayHasKey('business_website', $errors);
$this->assertEquals('Please enter a valid website URL', $errors['business_website']);
}
private function get_valid_test_data() {
return [
'first_name' => 'John',
'last_name' => 'Doe',
'business_name' => 'ACME HVAC',
'business_phone' => '123-456-7890',
'business_email' => 'test@example.com',
'user_country' => 'United States',
'user_state' => 'California',
'user_city' => 'Los Angeles',
'user_zip' => '90001',
'business_type' => 'Contractor',
'training_audience' => ['Anyone'],
'user_pass' => 'ValidPass1'
];
}
}