* @copyright (c) 2007-2016 Kohana Team
* @copyright (c) since 2016 Koseven Team
* @license https://koseven.dev/LICENSE
*/
class KO7_HTMLTest extends Unittest_TestCase {
/**
* Sets up the environment
*/
// @codingStandardsIgnoreStart
public function setUp(): void
// @codingStandardsIgnoreEnd
{
parent::setUp();
KO7::$config->load('url')->set('trusted_hosts', ['www\.koseven\.dev']);
}
/**
* Defaults for this test
* @var array
*/
// @codingStandardsIgnoreStart
protected $environmentDefault = [
'KO7::$base_url' => '/ko7/',
'KO7::$index_file' => 'index.php',
'HTML::$strict' => TRUE,
'HTTP_HOST' => 'www.koseven.dev',
];
// @codingStandardsIgnoreStart
/**
* Provides test data for test_attributes()
*
* @return array
*/
public function provider_attributes()
{
return [
[
['name' => 'field', 'random' => 'not_quite', 'id' => 'unique_field'],
[],
' id="unique_field" name="field" random="not_quite"'
],
[
['invalid' => NULL],
[],
''
],
[
[],
[],
''
],
[
['name' => 'field', 'checked'],
[],
' name="field" checked="checked"',
],
[
['id' => 'disabled_field', 'disabled'],
['HTML::$strict' => FALSE],
' id="disabled_field" disabled',
],
];
}
/**
* Tests HTML::attributes()
*
* @test
* @dataProvider provider_attributes
* @param array $attributes Attributes to use
* @param array $options Environment options to use
* @param string $expected Expected output
*/
public function test_attributes(array $attributes, array $options, $expected)
{
$this->setEnvironment($options);
$this->assertSame(
$expected,
HTML::attributes($attributes)
);
}
/**
* Provides test data for test_script
*
* @return array Array of test data
*/
public function provider_script()
{
return [
[
'',
'http://google.com/script.js',
],
[
'',
'my/script.js',
NULL,
'http',
TRUE
],
[
'',
'my/script.js',
NULL,
'https',
FALSE
],
[
'',
'/my/script.js', // Test absolute paths
NULL,
'https',
FALSE
],
[
'',
'//google.com/script.js',
],
];
}
/**
* Tests HTML::script()
*
* @test
* @dataProvider provider_script
* @param string $expected Expected output
* @param string $file URL to script
* @param array $attributes HTML attributes for the anchor
* @param string $protocol Protocol to use
* @param bool $index Should the index file be included in url?
*/
public function test_script($expected, $file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
{
$this->assertSame(
$expected,
HTML::script($file, $attributes, $protocol, $index)
);
}
/**
* Data provider for the style test
*
* @return array Array of test data
*/
public function provider_style()
{
return [
[
'',
'http://google.com/style.css',
[],
NULL,
FALSE
],
[
'',
'my/style.css',
[],
NULL,
FALSE
],
[
'',
'my/style.css',
[],
'https',
FALSE
],
[
'',
'my/style.css',
[],
'https',
TRUE
],
[
'',
'/my/style.css',
[],
'https',
TRUE
],
[
// #4283: http://koseven.dev/issues/4283
'',
'my/style.css',
[
'rel' => 'stylesheet/less'
],
'https',
TRUE
],
[
'',
'//google.com/style.css',
[],
NULL,
FALSE
],
];
}
/**
* Tests HTML::style()
*
* @test
* @dataProvider provider_style
* @param string $expected The expected output
* @param string $file The file to link to
* @param array $attributes Any extra attributes for the link
* @param string $protocol Protocol to use
* @param bool $index Whether the index file should be added to the link
*/
public function test_style($expected, $file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
{
$this->assertSame(
$expected,
HTML::style($file, $attributes, $protocol, $index)
);
}
/**
* Provides test data for test_anchor
*
* @return array Test data
*/
public function provider_anchor()
{
return [
// a fragment-only anchor
[
'KO7',
[],
'#go-to-section-ko7',
'KO7',
],
// a query-only anchor
[
'Category A',
[],
'?cat=a',
'Category A',
],
[
'KO7',
[],
'http://koseven.dev/',
'KO7',
],
[
'GOOGLE',
[],
'http://google.com',
'GOOGLE',
['target' => '_blank'],
'http',
],
[
'GOOGLE',
[],
'//google.com/',
'GOOGLE',
],
[
'KO7',
[],
'users/example',
'KO7',
NULL,
'https',
FALSE,
],
[
'KO7',
[],
'users/example',
'KO7',
NULL,
'https',
TRUE,
],
[
'KO7',
[],
'users/example',
'KO7',
NULL,
'https',
],
[
'KO7',
[],
'users/example',
'KO7',
NULL,
'https',
TRUE,
],
[
'KO7',
[],
'users/example',
'KO7',
NULL,
'https',
FALSE,
],
[
'KO7',
[],
'/users/example',
'KO7',
NULL,
'https',
FALSE,
],
];
}
/**
* Tests HTML::anchor
*
* @test
* @dataProvider provider_anchor
*/
public function test_anchor($expected, array $options, $uri, $title = NULL, array $attributes = NULL, $protocol = NULL, $index = TRUE)
{
// $this->setEnvironment($options);
$this->assertSame(
$expected,
HTML::anchor($uri, $title, $attributes, $protocol, $index)
);
}
/**
* Data provider for test_file_anchor
*
* @return array
*/
public function provider_file_anchor()
{
return [
[
'My picture file',
[],
'mypic.png',
'My picture file',
],
[
'My picture file',
['attr' => 'value'],
'mypic.png',
'My picture file',
'https',
TRUE
],
[
'My picture file',
[],
'mypic.png',
'My picture file',
'ftp',
FALSE
],
[
'My picture file',
[],
'/mypic.png',
'My picture file',
'ftp',
FALSE
],
];
}
/**
* Test for HTML::file_anchor()
*
* @test
* @covers HTML::file_anchor
* @dataProvider provider_file_anchor
*/
public function test_file_anchor($expected, array $attributes, $file, $title = NULL, $protocol = NULL, $index = FALSE)
{
$this->assertSame(
$expected,
HTML::file_anchor($file, $title, $attributes, $protocol, $index)
);
}
/**
* Provides test data for test_image
*
* @return array Array of test data
*/
public function provider_image()
{
return [
[
'',
'http://google.com/image.png',
],
[
'',
'//google.com/image.png',
],
[
'',
'img/image.png',
],
[
'',
'img/image.png',
['alt' => '...',],
'https',
TRUE
],
];
}
/**
* Tests HTML::image()
*
* @test
* @dataProvider provider_image
* @param string $expected Expected output
* @param string $file file name
* @param array $attributes HTML attributes for the image
* @param string $protocol Protocol to use
* @param bool $index Should the index file be included in url?
*/
public function test_image($expected, $file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
{
$this->assertSame(
$expected,
HTML::image($file, $attributes, $protocol, $index)
);
}
}