File: //opt/cloudlinux/alt-php56/root/usr/share/pear/test/HTML_Common2/HTML_Common2_Test.php
<?php
/**
* Unit test suite for HTML_Common2 class
*
* PHP version 5
*
* LICENSE:
*
* Copyright (c) 2004-2022, Alexey Borzov <avb@php.net>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category HTML
* @package HTML_Common2
* @author Alexey Borzov <avb@php.net>
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @link https://pear.php.net/package/HTML_Common2
*/
/** Sets up includes */
require_once __DIR__ . '/TestHelper.php';
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
/**
* Unit test for HTML_Common2 class
*/
class HTML_Common2_Test extends TestCase
{
public function testUnknownOptionIsNull()
{
$this->assertNull(HTML_Common2::getOption('foobar'));
}
public function testAnyOptionAllowed()
{
HTML_Common2::setOption('foobar', 'baz');
$this->assertEquals('baz', HTML_Common2::getOption('foobar'));
}
public function testArrayOfOptionsAllowed()
{
HTML_Common2::setOption([
'quux' => 'xyzzy'
]);
$this->assertEquals('xyzzy', HTML_Common2::getOption('quux'));
$this->assertArrayHasKey(HTML_Common2::OPTION_CHARSET, HTML_Common2::getOption());
}
public function testConstructorSetsDefaultAttributes()
{
$obj = new Common2Impl();
$this->assertEquals([], $obj->getAttributes());
$obj = new Common2Impl(['foo' => 'bar']);
$this->assertEquals(['foo' => 'bar'], $obj->getAttributes());
}
public function testUnknownAttributeIsNull()
{
$obj = new Common2Impl();
$this->assertNull($obj->getAttribute('foobar'));
}
public function testAttributeNamesAreLowercased()
{
$obj = new Common2Impl();
$obj->setAttributes(['BAZ' => 'quux']);
$obj->setAttribute('Foo', 'bar');
$obj->mergeAttributes(['XyZZy' => 'xyzzy value']);
$this->assertEquals('bar', $obj->getAttribute('FOO'));
$obj->removeAttribute('fOO');
$this->assertEquals(
['baz' => 'quux', 'xyzzy' => 'xyzzy value'],
$obj->getAttributes()
);
}
public function testAttributeValuesAreStrings()
{
$obj = new Common2Impl();
$obj->setAttributes(['foo' => null, 'bar' => 10]);
$obj->setAttribute('baz', 2.5);
$obj->mergeAttributes(['foobar' => 42]);
foreach ($obj->getAttributes() as $attribute) {
$this->assertIsString($attribute);
}
}
public function testDefaultIndentLevelIsZero()
{
$obj = new Common2Impl();
$this->assertEquals(0, $obj->getIndentLevel());
}
public function testIndentLevelIsNonnegative()
{
$obj = new Common2Impl();
$obj->setIndentLevel(-1);
$this->assertEquals(0, $obj->getIndentLevel());
$obj->setIndentLevel(1);
$this->assertEquals(1, $obj->getIndentLevel());
}
public function testDefaultCommentIsNull()
{
$obj = new Common2Impl();
$this->assertNull($obj->getComment());
}
public function testAttributesAsStringAccepted()
{
$obj = new Common2Impl('multiple style= "height: 2em;" class=\'foo\' width=100% ');
$this->assertEquals(
['multiple' => 'multiple', 'style' => 'height: 2em;',
'class' => 'foo', 'width' => '100%'],
$obj->getAttributes()
);
}
public function testNonXhtmlAttributesTransformed()
{
$obj = new Common2Impl(['multiple']);
$obj->setAttribute('selected');
$obj->mergeAttributes('checked nowrap');
$this->assertEquals(
['multiple' => 'multiple', 'selected' => 'selected',
'checked' => 'checked', 'nowrap' => 'nowrap'],
$obj->getAttributes()
);
}
public function testWellFormedXhtmlGenerated()
{
$obj = new Common2Impl(['foo' => 'bar&"baz"', 'quux' => 'xyz\'zy']);
$this->assertEquals(
' foo="bar&"baz"" quux="xyz'zy"',
$obj->getAttributes(true)
);
}
public function testCanWatchAttributes()
{
$obj = new WatchedAttributes();
$obj->setAttributes(['readonly' => 'something', 'uppercase' => 'new value', 'foo' => 'bar']);
$this->assertEquals(
['readonly' => 'this attribute is readonly', 'uppercase' => 'NEW VALUE', 'foo' => 'bar'],
$obj->getAttributes()
);
$obj->mergeAttributes(['readonly' => 'something', 'uppercase' => 'other value', 'foo' => 'baz']);
$this->assertEquals(
['readonly' => 'this attribute is readonly', 'uppercase' => 'OTHER VALUE', 'foo' => 'baz'],
$obj->getAttributes()
);
$obj->setAttribute('readonly', 'something else');
$obj->setAttribute('uppercase', 'yet another value');
$obj->setAttribute('foo', 'quux');
$this->assertEquals(
['readonly' => 'this attribute is readonly', 'uppercase' => 'YET ANOTHER VALUE', 'foo' => 'quux'],
$obj->getAttributes()
);
$obj->removeAttribute('readonly');
$obj->removeAttribute('uppercase');
$obj->removeAttribute('foo');
$this->assertEquals(
['readonly' => 'this attribute is readonly'],
$obj->getAttributes()
);
}
public function testFluentInterfaces()
{
$obj = new Common2Impl();
$this->assertSame($obj, $obj->setAttributes(['foo' => 'foo value']));
$this->assertSame($obj, $obj->mergeAttributes(['bar' => 'bar value']));
$this->assertSame($obj, $obj->setAttribute('baz', 'baz value'));
$this->assertSame($obj, $obj->removeAttribute('bar'));
$this->assertSame($obj, $obj->setComment('A comment'));
$this->assertSame($obj, $obj->setIndentLevel(3));
}
public function testCanAddCssClasses()
{
$obj = new Common2Impl();
$obj->addClass('foobar');
$obj->addClass('foobar');
$this->assertEquals('foobar', $obj->getAttribute('class'));
$obj->addClass('quux xyzzy');
$this->assertFalse($obj->hasClass('bar'));
$this->assertTrue($obj->hasClass('foobar'));
$this->assertTrue($obj->hasClass('quux'));
$this->assertTrue($obj->hasClass('xyzzy'));
$obj->addClass(['newclass']);
$this->assertTrue($obj->hasClass('newclass'));
}
public function testCanRemoveCssClasses()
{
$obj = new Common2Impl(['class' => 'foobar quux xyzzy']);
$obj->removeClass('foobar xyzzy');
$this->assertFalse($obj->hasClass('xyzzy'));
$this->assertFalse($obj->hasClass('foobar'));
$this->assertTrue($obj->hasClass('quux'));
$obj->removeClass(['quux']);
$this->assertEquals(null, $obj->getAttribute('class'));
}
public function testArrayAccess()
{
$obj = new Common2Impl(['baz' => 'quux']);
$this->assertTrue(isset($obj['baz']));
$this->assertEquals('quux', $obj['baz']);
$obj['foo'] = 'bar';
$this->assertTrue(isset($obj['foo']));
$this->assertEquals('bar', $obj['foo']);
unset($obj['fOo']);
$this->assertFalse(isset($obj['foo']));
$obj[] = 'disabled';
$this->assertTrue(isset($obj['disabled']));
$this->assertEquals('disabled', $obj['disabled']);
}
}
?>