🆙 Add cms i using 🆙

This commit is contained in:
Remco
2025-11-25 22:42:56 +01:00
parent 94704e0925
commit d44196149e
35591 changed files with 3601123 additions and 0 deletions
@@ -0,0 +1,34 @@
<?php
namespace Stevebauman\Purify\Tests;
use Stevebauman\Purify\Cache\CacheDefinitionCache;
use Stevebauman\Purify\Facades\Purify;
use Symfony\Component\Finder\Finder;
class CacheDefinitionCacheTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->app['config']->set('purify.serializer', [
'driver' => 'file',
'cache' => CacheDefinitionCache::class,
]);
}
public function test_cache_can_be_used()
{
Purify::clean('foo');
$dir = $this->app['config']->get('cache.stores.file.path');
$this->assertTrue(
Finder::create()
->in($dir)
->depth(0)
->hasResults()
);
}
}
@@ -0,0 +1,106 @@
<?php
namespace Stevebauman\Purify\Tests;
use Illuminate\Database\Eloquent\Model;
use Stevebauman\Purify\Casts\PurifyHtmlOnGet;
use Stevebauman\Purify\Casts\PurifyHtmlOnSet;
class CastsTest extends TestCase
{
public $testInput = '<script>alert("Harmful Script");</script><p style="a {color: #0000ff;}" class="a-different-class">Test<span>bar</span></p>';
public function test_purifies_on_get_with_default_config()
{
$this->app['config']->set('purify.configs.default', [
'HTML.Allowed' => 'p',
]);
$model = new PurifyingDefaultOnGetModel();
$model->body = $this->testInput;
$this->assertEquals($this->testInput, $model->getAttributes()['body']);
$this->assertEquals('<p>Testbar</p>', $model->body);
}
public function test_purifies_on_get_with_custom_config()
{
$this->app['config']->set('purify.configs.foo', [
'HTML.Allowed' => 'p,span',
]);
$model = new PurifyingFooOnGetModel();
$model->body = $this->testInput;
$this->assertEquals($this->testInput, $model->getAttributes()['body']);
$this->assertEquals('<p>Test<span>bar</span></p>', $model->body);
}
public function test_returns_null_on_get_when_value_is_null()
{
$model = new PurifyingDefaultOnGetModel();
$model->body = null;
$this->assertNull($model->body);
}
public function test_purifies_on_set_with_default_config()
{
$this->app['config']->set('purify.configs.default', [
'HTML.Allowed' => 'p',
]);
$model = new PurifyingDefaultOnSetModel();
$model->body = $this->testInput;
$this->assertEquals('<p>Testbar</p>', $model->getAttributes()['body']);
}
public function test_purifies_on_set_with_custom_config()
{
$this->app['config']->set('purify.configs.foo', [
'HTML.Allowed' => 'p,span',
]);
$model = new PurifyingFooOnSetModel();
$model->body = $this->testInput;
$this->assertEquals('<p>Test<span>bar</span></p>', $model->getAttributes()['body']);
}
public function test_sets_null_on_set_when_value_is_null()
{
$model = new PurifyingDefaultOnSetModel();
$model->body = null;
$this->assertNull($model->getAttributes()['body']);
}
}
class PurifyingDefaultOnGetModel extends Model
{
protected $casts = [
'body' => PurifyHtmlOnGet::class,
];
}
class PurifyingFooOnGetModel extends Model
{
protected $casts = [
'body' => PurifyHtmlOnGet::class.':foo',
];
}
class PurifyingDefaultOnSetModel extends Model
{
protected $casts = [
'body' => PurifyHtmlOnSet::class,
];
}
class PurifyingFooOnSetModel extends Model
{
protected $casts = [
'body' => PurifyHtmlOnSet::class.':foo',
];
}
@@ -0,0 +1,38 @@
<?php
namespace Stevebauman\Purify\Tests;
use Illuminate\Support\Facades\Storage;
use Stevebauman\Purify\Cache\FilesystemDefinitionCache;
use Stevebauman\Purify\Commands\ClearCommand;
use Stevebauman\Purify\Facades\Purify;
use Symfony\Component\Finder\Finder;
class FilesystemDefinitionCacheTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->app['config']->set('purify.serializer', [
'disk' => 'local',
'path' => 'purify',
'cache' => FilesystemDefinitionCache::class,
]);
$this->artisan(ClearCommand::class);
}
public function test_filesystem_can_be_used()
{
Purify::clean('foo');
$dir = $this->app['config']->get('purify.serializer.path');
$this->assertTrue(
Finder::create()->in(
Storage::path($dir)
)->depth(0)->hasResults()
);
}
}
@@ -0,0 +1,26 @@
<?php
namespace Stevebauman\Purify\Tests;
use Illuminate\Support\Facades\Storage;
use Stevebauman\Purify\Commands\ClearCommand;
use Stevebauman\Purify\Facades\Purify;
class NullDefinitionCacheTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->artisan(ClearCommand::class);
$this->app['config']->set('purify.serializer', null);
}
public function test_null_cache_can_be_used()
{
Purify::clean('foo');
$this->assertEmpty(Storage::allFiles('purfiy'));
}
}
@@ -0,0 +1,185 @@
<?php
namespace Stevebauman\Purify\Tests;
use HTMLPurifier_CSSDefinition;
use HTMLPurifier_HTMLDefinition;
use Illuminate\Support\Facades\File;
use Stevebauman\Purify\Cache\CacheDefinitionCache;
use Stevebauman\Purify\Definitions\CssDefinition;
use Stevebauman\Purify\Definitions\Definition;
use Stevebauman\Purify\Facades\Purify;
use Stevebauman\Purify\PurifyServiceProvider;
class PurifyTest extends TestCase
{
public $testInput = '<script>alert("Harmful Script");</script><p style="a {color: #0000ff;}" class="a-different-class">Test</p>';
protected function setUp(): void
{
parent::setUp();
$this->app['config']->set('purify.serializer', [
'driver' => 'file',
'cache' => CacheDefinitionCache::class,
]);
}
public function test_configuration_file_is_published()
{
$this->artisan('vendor:publish', ['--provider' => PurifyServiceProvider::class]);
$this->assertFileExists(config_path('purify.php'));
File::delete(config_path('purify.php'));
File::deleteDirectory(storage_path('app/purify'));
}
public function test_input_is_sanitized()
{
$cleaned = Purify::clean($this->testInput);
$expected = '<p>Test</p>';
$this->assertEquals($expected, $cleaned);
}
public function test_input_arrays_are_sanitized()
{
$cleaned = Purify::clean([$this->testInput, $this->testInput]);
$expected = ['<p>Test</p>', '<p>Test</p>'];
$this->assertEquals($expected, $cleaned);
}
public function test_config_alias_is_available()
{
$instance = Purify::config();
$this->assertInstanceOf(\Stevebauman\Purify\Purify::class, $instance);
}
public function test_config_set_can_be_chosen()
{
$input = '<a href="http://www.google.ca">Google</a>';
$this->app['config']->set('purify.configs.foo', [
'HTML.TargetBlank' => true,
]);
$cleaned = Purify::driver('foo')->clean($input);
$expected = '<a href="http://www.google.ca" target="_blank" rel="noreferrer noopener">Google</a>';
$this->assertEquals($expected, $cleaned);
}
public function test_config_can_be_provided_inline()
{
$input = '<a href="http://www.google.ca">Google</a>';
$cleaned = Purify::config([
'HTML.TargetBlank' => true,
])->clean($input);
$expected = '<a href="http://www.google.ca" target="_blank" rel="noreferrer noopener">Google</a>';
$this->assertEquals($expected, $cleaned);
}
public function test_configs_are_independent()
{
$input = '<a href="http://www.google.ca">Google</a>';
$this->app['config']->set('purify.configs.foo', [
'HTML.TargetBlank' => true,
]);
$this->app['config']->set('purify.configs.bar', [
'HTML.TargetBlank' => true,
'HTML.TargetNoopener' => false,
]);
$cleaned1 = Purify::clean($input);
$cleaned2 = Purify::driver('foo')->clean($input);
$cleaned3 = Purify::driver('bar')->clean($input);
$expected1 = '<a href="http://www.google.ca">Google</a>';
$expected2 = '<a href="http://www.google.ca" target="_blank" rel="noreferrer noopener">Google</a>';
$expected3 = '<a href="http://www.google.ca" target="_blank" rel="noreferrer">Google</a>';
$this->assertEquals($expected1, $cleaned1);
$this->assertEquals($expected2, $cleaned2);
$this->assertEquals($expected3, $cleaned3);
}
public function test_custom_definitions_are_applied()
{
$this->app['config']->set('purify.definitions', FooDefinition::class);
$this->assertEquals(
'<span>Test</span>',
Purify::clean('<span class="foo">Test</span>')
);
$this->assertEquals(
'<span class="foo">Test</span>',
Purify::config(['HTML.Allowed' => 'span[class]'])->clean('<span class="foo">Test</span>')
);
$this->assertEquals(
'<span>Test</span>',
Purify::config(['HTML.Allowed' => 'span[class]'])->clean('<span class="bar">Test</span>')
);
}
public function test_custom_css_definitions_are_applied()
{
$this->app['config']->set('purify.css-definitions', FooCssDefinition::class);
$this->assertEquals(
'<p>Test</p>',
Purify::clean('<p style="text-align:left">Test</p>')
);
$this->assertEquals(
'<p>Test</p>',
Purify::clean('<p style="text-align:right">Test</p>')
);
$this->assertEquals(
'<p style="text-align:center;">Test</p>',
Purify::clean('<p style="text-align:center;">Test</p>')
);
$this->assertEquals(
'<p style="text-align:start;">Test</p>',
Purify::clean('<p style="text-align:start;">Test</p>')
);
$this->assertEquals(
'<p style="text-align:end;">Test</p>',
Purify::clean('<p style="text-align:end;">Test</p>')
);
}
}
class FooDefinition implements Definition
{
public static function apply(HTMLPurifier_HTMLDefinition $definition)
{
$definition->addAttribute('span', 'class', 'Enum#foo');
}
}
class FooCssDefinition implements CssDefinition
{
public static function apply(HTMLPurifier_CSSDefinition $definition)
{
$definition->info['text-align'] = new \HTMLPurifier_AttrDef_Enum(
['center', 'start', 'end'],
false,
);
}
}
@@ -0,0 +1,28 @@
<?php
namespace Stevebauman\Purify\Tests;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Stevebauman\Purify\Commands\ClearCommand;
use Stevebauman\Purify\PurifyManager;
use Stevebauman\Purify\PurifyServiceProvider;
class TestCase extends BaseTestCase
{
protected function tearDown(): void
{
$this->artisan(ClearCommand::class);
parent::tearDown();
}
protected function getPackageAliases($app)
{
return ['Purify' => PurifyManager::class];
}
protected function getPackageProviders($app)
{
return [PurifyServiceProvider::class];
}
}