cms/tests/Feature/ThemeHelperTest.php

98 lines
3.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\File;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ThemeHelperTest extends TestCase
{
use RefreshDatabase;
protected string $themesPath;
protected function setUp(): void
{
parent::setUp();
$this->themesPath = base_path('themes');
if (! File::exists($this->themesPath)) {
File::makeDirectory($this->themesPath);
}
if (! File::exists($this->themesPath . '/test-theme')) {
File::makeDirectory($this->themesPath . '/test-theme');
}
File::put($this->themesPath . '/test-theme/theme.md', "title: Test Theme");
\App\Models\Setting::set('active_theme', 'test-theme', 'cms');
Config::set('app.url', 'http://localhost');
}
protected function tearDown(): void
{
File::deleteDirectory($this->themesPath . '/test-theme');
parent::tearDown();
}
public function test_css_helper_generates_correct_tag(): void
{
$html = css('css/style.css', ['id' => 'main-css', 'media' => 'all']);
$this->assertStringContainsString('<link rel="stylesheet"', $html);
$this->assertStringContainsString('href="http://localhost/themes/test-theme/css/style.css"', $html);
$this->assertStringContainsString('id="main-css"', $html);
$this->assertStringContainsString('media="all"', $html);
}
public function test_js_helper_generates_correct_tag(): void
{
$html = js('js/app.js', ['defer' => true, 'async' => true]);
$this->assertStringContainsString('<script src="http://localhost/themes/test-theme/js/app.js"', $html);
$this->assertStringContainsString('defer', $html);
$this->assertStringContainsString('async', $html);
$this->assertStringContainsString('></script>', $html);
}
public function test_sw_file_generates_img_tag_for_images(): void
{
$html = sw_file('img/logo.png', ['alt' => 'Site Logo', 'class' => 'ui image']);
$this->assertStringContainsString('<img src="http://localhost/themes/test-theme/img/logo.png"', $html);
$this->assertStringContainsString('alt="Site Logo"', $html);
$this->assertStringContainsString('class="ui image"', $html);
}
public function test_sw_file_handles_jit_parameters(): void
{
$html = sw_file('img/hero.jpg', ['w' => 800, 'h' => 400, 'fit' => 'crop', 'fm' => 'webp']);
$this->assertStringContainsString('w=800', $html);
$this->assertStringContainsString('h=400', $html);
$this->assertStringContainsString('fit=crop', $html);
$this->assertStringContainsString('fm=webp', $html);
$this->assertStringContainsString('<img src="http://localhost/themes/test-theme/img/hero.jpg?', $html);
}
public function test_sw_file_generates_video_and_audio_tags(): void
{
$video = sw_file('video/promo.mp4', ['controls' => true]);
$this->assertStringContainsString('<video src="http://localhost/themes/test-theme/video/promo.mp4"', $video);
$this->assertStringContainsString('controls', $video);
$audio = sw_file('audio/podcast.mp3', ['autoplay' => false]);
$this->assertStringContainsString('<audio src="http://localhost/themes/test-theme/audio/podcast.mp3"', $audio);
}
public function test_sw_file_defaults_to_link(): void
{
$html = sw_file('docs/manual.pdf', ['text' => 'Download Manual', 'target' => '_blank']);
$this->assertStringContainsString('<a href="http://localhost/themes/test-theme/docs/manual.pdf"', $html);
$this->assertStringContainsString('target="_blank"', $html);
$this->assertStringContainsString('Download Manual</a>', $html);
}
}