1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
<?php
declare(strict_types=1);
namespace Shaarli\NetscapeBookmarkParser\Tests\Unit\Encoder;
use PHPUnit\Framework\TestCase;
use Shaarli\NetscapeBookmarkParser\Encoder\NetscapeBookmarkDecoder;
class NetscapeBookmarkDecoderTest extends TestCase
{
/**
* @var NetscapeBookmarkDecoder|null
*/
private $decoder;
/**
* Parse boolean attribute values evaluating to true.
*
* Standard booleans: '1|on|one|t|true|y|yes'
* HTML forms: 'checked|ok|okay'
* other: '\+'
* integers != [0, 1] => true: These are removed
*/
public function testParseBooleanAttributesAsTrue(): void
{
foreach (
[
'+',
'1',
'array',
'checked',
'ok',
'okay',
'on',
'one',
't',
'true',
'y',
'yes',
] as $value
) {
$this->assertTrue($this->decoder->parseBoolean($value));
}
}
/**
* Parse boolean attribute values evaluating to false.
*
* Standard booleans: '0|f|false|n|neg|nil|no|off|zero'
* Empty values: 'empty|null|void'
* Errors: 'die|exit'
* Other: '\-'
*/
public function testParseBooleanAttributesAsFalse(): void
{
foreach (
[
'-',
'0',
'die',
'empty',
'exit',
'f',
'false',
'n',
'neg',
'nil',
'no',
'null',
'off',
'void',
'zero',
] as $value
) {
$this->assertFalse($this->decoder->parseBoolean($value));
}
}
/**
* Parse boolean attribute values, fail and return false.
*/
public function testParseInvalidBooleanAttributes(): void
{
foreach (
[
'+++',
'--',
'nope',
'yess',
'yup',
] as $value
) {
$this->assertFalse($this->decoder->parseBoolean($value));
}
}
/**
* Parse log dates.
*/
public function testParseLogDates(): void
{
$this->assertEquals(
'971211336',
$this->decoder->parseDate(
'10/Oct/2000:13:55:36 -0700',
NetscapeBookmarkDecoder::DEFAULT_NORMALIZE_DATES,
NetscapeBookmarkDecoder::DEFAULT_DATE_RANGE
)
);
$this->assertEquals(
'971186136',
$this->decoder->parseDate(
'10/Oct/2000:13:55:36 +0000',
NetscapeBookmarkDecoder::DEFAULT_NORMALIZE_DATES,
NetscapeBookmarkDecoder::DEFAULT_DATE_RANGE
)
);
$this->assertEquals(
'971175336',
$this->decoder->parseDate(
'10/Oct/2000:13:55:36 +0300',
NetscapeBookmarkDecoder::DEFAULT_NORMALIZE_DATES,
NetscapeBookmarkDecoder::DEFAULT_DATE_RANGE
)
);
}
/**
* Parse Unix timestamps.
*/
public function testParseUnixDates(): void
{
$this->assertEquals(
'1456433748',
$this->decoder->parseDate(
'1456433748',
NetscapeBookmarkDecoder::DEFAULT_NORMALIZE_DATES,
NetscapeBookmarkDecoder::DEFAULT_DATE_RANGE
)
);
$this->assertEquals(
'971175336',
$this->decoder->parseDate(
'971175336',
NetscapeBookmarkDecoder::DEFAULT_NORMALIZE_DATES,
NetscapeBookmarkDecoder::DEFAULT_DATE_RANGE
)
);
$this->assertEquals(
'-371211336',
$this->decoder->parseDate(
'-371211336',
NetscapeBookmarkDecoder::DEFAULT_NORMALIZE_DATES,
NetscapeBookmarkDecoder::DEFAULT_DATE_RANGE
)
);
}
/**
* Parse invalid date.
*/
public function testParseInvalidDates(): void
{
// Just to make sure that even if the CI is slow, we're fine.
$range = [
time() - 5,
time() + 5,
];
$date = $this->decoder->parseDate(
'0',
NetscapeBookmarkDecoder::DEFAULT_NORMALIZE_DATES,
NetscapeBookmarkDecoder::DEFAULT_DATE_RANGE
);
$this->assertGreaterThanOrEqual($range[0], $date);
$this->assertLessThan($range[1], $date);
$date = $this->decoder->parseDate(
'not a date',
NetscapeBookmarkDecoder::DEFAULT_NORMALIZE_DATES,
NetscapeBookmarkDecoder::DEFAULT_DATE_RANGE
);
$this->assertGreaterThanOrEqual($range[0], $date);
$this->assertLessThan($range[1], $date);
}
/**
* Sanitize tags derived from bookmark folder names.
*/
public function testSanitizeFolderTagString(): void
{
$this->assertEquals([], $this->decoder->sanitizeTags(''));
$this->assertEquals(
['hello'],
$this->decoder->sanitizeTags('-hello')
);
$this->assertEquals(
['hello'],
$this->decoder->sanitizeTags('_hello')
);
$this->assertEquals(
[
'hello',
'world',
],
$this->decoder->sanitizeTags('-hello$, WOrld')
);
$this->assertEquals(
[
'hello',
'world',
],
$this->decoder->sanitizeTags('-Hello$ - WOrlD!')
);
}
// --------------------------------------------------
// Setup / Tear Down
// --------------------------------------------------
protected function setUp(): void
{
$this->decoder = new NetscapeBookmarkDecoder();
}
protected function tearDown(): void
{
$this->decoder = null;
}
}
|