Skip to content

Commit

Permalink
Make parse() public, add make() impl
Browse files Browse the repository at this point in the history
  • Loading branch information
xemlock committed May 4, 2019
1 parent 20c75f8 commit 77c4320
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
12 changes: 11 additions & 1 deletion library/HTMLPurifier/AttrDef/HTML5/Datetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function validate($string, $config, $context)
* @param string $string
* @return array|bool
*/
protected function parse($string)
public function parse($string)
{
$string = $this->parseCDATA($string);

Expand Down Expand Up @@ -348,4 +348,14 @@ protected function formatTimezoneOffset(array $data)

return $tzOffset;
}

/**
* @noinspection PhpDocMissingThrowsInspection
* @param string $formats
* @return HTMLPurifier_AttrDef_HTML5_Datetime
*/
public function make($formats)
{
return new self(explode(',', $formats));
}
}
56 changes: 42 additions & 14 deletions tests/HTMLPurifier/AttrDef/HTML5/DatetimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class HTMLPurifier_AttrDef_HTML5_DatetimeTest extends BaseTestCase
*/
protected $context;

/**
* @var HTMLPurifier_AttrDef_HTML5_Datetime
*/
protected $attr;

/**
* @var string
*/
Expand All @@ -30,17 +35,28 @@ protected function tearDown()
}

/**
* @param string|array $formats
* @param HTMLPurifier_AttrDef_HTML5_Datetime|string|string[] $attr
* @return $this
*/
public function attr($attr = null)
{
if ($attr instanceof HTMLPurifier_AttrDef_HTML5_Datetime) {
$this->attr = $attr;
} else {
$this->attr = new HTMLPurifier_AttrDef_HTML5_Datetime((array) $attr);
}
return $this;
}

/**
* @param string $input
* @param string $expected OPTIONAL
*/
public function assertValidate($formats, $input, $expected = null)
public function assertValidate($input, $expected = null)
{
$attr = new HTMLPurifier_AttrDef_HTML5_Datetime((array) $formats);

$this->assertEquals(
$expected === null ? $input : $expected,
$attr->validate($input, $this->config, $this->context)
$this->attr->validate($input, $this->config, $this->context)
);
}

Expand All @@ -51,7 +67,7 @@ public function assertValidate($formats, $input, $expected = null)
*/
public function testYear($input, $expected = null)
{
$this->assertValidate('Year', $input, $expected);
$this->attr('Year')->assertValidate($input, $expected);
}

public function yearData()
Expand Down Expand Up @@ -82,7 +98,7 @@ public function yearData()
*/
public function testMonth($input, $expected = null)
{
$this->assertValidate('Month', $input, $expected);
$this->attr('Month')->assertValidate($input, $expected);
}

public function monthData()
Expand Down Expand Up @@ -115,7 +131,7 @@ public function monthData()
*/
public function testDate($input, $expected = null)
{
$this->assertValidate('Date', $input, $expected);
$this->attr('Date')->assertValidate($input, $expected);
}

public function dateData()
Expand Down Expand Up @@ -147,7 +163,7 @@ public function dateData()
*/
public function testTime($input, $expected = null)
{
$this->assertValidate('Time', $input, $expected);
$this->attr('Time')->assertValidate($input, $expected);
}

public function timeData()
Expand Down Expand Up @@ -180,7 +196,7 @@ public function timeData()
*/
public function testDatetimeLocal($input, $expected = null)
{
$this->assertValidate('DatetimeLocal', $input, $expected);
$this->attr('DatetimeLocal')->assertValidate($input, $expected);
}

public function datetimeLocalData()
Expand Down Expand Up @@ -223,7 +239,7 @@ public function datetimeLocalData()
*/
public function testDatetimeGlobal($input, $expected = null)
{
$this->assertValidate('DatetimeGlobal', $input, $expected);
$this->attr('DatetimeGlobal')->assertValidate($input, $expected);
}

public function datetimeGlobalData()
Expand Down Expand Up @@ -279,7 +295,7 @@ public function datetimeGlobalData()
*/
public function testDatetime($input, $expected = null)
{
$this->assertValidate('Datetime', $input, $expected);
$this->attr('Datetime')->assertValidate($input, $expected);
}

public function datetimeData()
Expand Down Expand Up @@ -335,7 +351,7 @@ public function datetimeData()
*/
public function testTimezoneOffset($input, $expected = null)
{
$this->assertValidate('TimezoneOffset', $input, $expected);
$this->attr('TimezoneOffset')->assertValidate($input, $expected);
}

public function timezoneOffsetData()
Expand Down Expand Up @@ -367,7 +383,7 @@ public function timezoneOffsetData()
*/
public function testDefault($input, $expected = null)
{
$this->assertValidate(null, $input, $expected);
$this->attr()->assertValidate($input, $expected);
}

public function defaultData()
Expand Down Expand Up @@ -470,6 +486,18 @@ public function defaultData()
);
}

public function testMake()
{
$factory = new HTMLPurifier_AttrDef_HTML5_Datetime();
$attr = $factory->make('Time,Datetime');

$this->assertInstanceOf(get_class($factory), $attr);

$this->attr = $attr;
$this->assertValidate('21:37');
$this->assertValidate('2005-04-02 21:37');
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage not a valid format
Expand Down

0 comments on commit 77c4320

Please sign in to comment.