<?php
declare(strict_types=1);

namespace App\Test\TestCase\Controller;

use Cake\Core\Configure;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;

class PagesControllerTest extends TestCase
{
    use IntegrationTestTrait;

    public function testHomePageRendersCandleCraftContent(): void
    {
        $this->get('/');

        $this->assertResponseOk();
        $this->assertResponseContains('CandleCraft');
        $this->assertResponseContains('Make something');
        $this->assertResponseContains('Send an enquiry');
        $this->assertResponseContains('Book a class');
    }

    public function testRouteHelpPageRendersRequestedPath(): void
    {
        $this->get('/route-help?from=%2Fnot-real');

        $this->assertResponseOk();
        $this->assertResponseContains("That page isn't here");
        $this->assertResponseContains('/not-real');
        $this->assertResponseContains('Send a quick note');
    }

    public function testMissingTemplate(): void
    {
        Configure::write('debug', false);
        $this->get('/pages/not_existing');

        $this->assertRedirectContains('/route-help?from=%2Fpages%2Fnot_existing');
    }

    public function testMissingTemplateInDebug(): void
    {
        Configure::write('debug', true);
        $this->get('/pages/not_existing');

        $this->assertResponseFailure();
        $this->assertResponseContains('Missing Template');
        $this->assertResponseContains('not_existing.php');
    }

    public function testDirectoryTraversalProtection(): void
    {
        $this->get('/pages/../Layout/ajax');
        $this->assertResponseCode(403);
        $this->assertResponseContains('Forbidden');
    }

    public function testUnknownUrlRedirectsToRouteHelp(): void
    {
        $this->get('/definitely-not-a-real-page');

        $this->assertRedirectContains('/route-help?from=%2Fdefinitely-not-a-real-page');
    }
}
