83 lines
2.4 KiB
PHP
83 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\App\Controllers\View {
|
|
|
|
use App\Controllers\View\RegisterController;
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
|
use CodeIgniter\Test\CIUnitTestCase;
|
|
use Config\Services;
|
|
|
|
class TestableRegisterController extends RegisterController
|
|
{
|
|
public function __construct()
|
|
{
|
|
// Skip the heavy constructor that connects to the real database.
|
|
}
|
|
}
|
|
|
|
class RegisterControllerTest extends CIUnitTestCase
|
|
{
|
|
private TestableRegisterController $controller;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
Services::resetSingle('session');
|
|
session()->start();
|
|
|
|
$this->controller = new TestableRegisterController();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
session()->remove('captcha_answer');
|
|
session()->remove('user_email');
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function testIndexGeneratesCaptchaWhenMissing()
|
|
{
|
|
session()->remove('captcha_answer');
|
|
|
|
$response = $this->controller->index();
|
|
|
|
$this->assertIsString($response);
|
|
$captcha = session()->get('captcha_answer');
|
|
$this->assertNotEmpty($captcha);
|
|
$this->assertGreaterThanOrEqual(4, strlen($captcha));
|
|
$this->assertLessThanOrEqual(8, strlen($captcha));
|
|
}
|
|
|
|
public function testIndexReusesExistingCaptcha()
|
|
{
|
|
session()->set('captcha_answer', 'EXISTING');
|
|
|
|
$response = $this->controller->index();
|
|
|
|
$this->assertIsString($response);
|
|
$this->assertSame('EXISTING', session()->get('captcha_answer'));
|
|
}
|
|
|
|
public function testSuccessRedirectsWhenEmailMissing()
|
|
{
|
|
session()->remove('user_email');
|
|
|
|
$response = $this->controller->success();
|
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response);
|
|
$this->assertStringContainsString('/register', $response->getHeaderLine('Location') ?: '');
|
|
}
|
|
|
|
public function testSuccessReturnsViewWhenEmailPresent()
|
|
{
|
|
session()->set('user_email', 'parent@example.com');
|
|
|
|
$response = $this->controller->success();
|
|
|
|
$this->assertIsString($response);
|
|
$this->assertSame('parent@example.com', session()->get('user_email'));
|
|
}
|
|
}
|
|
}
|