Skip to content

Commit af418c2

Browse files
authored
Merge branch 'master' into master
2 parents 2108727 + 106103c commit af418c2

9 files changed

Lines changed: 190 additions & 6 deletions

Index.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ public function write()
120120
public function setUseGzip($value)
121121
{
122122
if ($value && !extension_loaded('zlib')) {
123+
// @codeCoverageIgnoreStart
123124
throw new \RuntimeException('Zlib extension must be installed to gzip the sitemap.');
125+
// @codeCoverageIgnoreEnd
124126
}
125127
$this->useGzip = $value;
126128
}
@@ -140,4 +142,4 @@ public function setStylesheet($stylesheetUrl)
140142
$this->stylesheet = $stylesheetUrl;
141143
}
142144
}
143-
}
145+
}

Sitemap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ private function createNewFile()
155155
if (function_exists('deflate_init') && function_exists('deflate_add')) {
156156
$this->writerBackend = new DeflateWriter($filePath);
157157
} else {
158+
// @codeCoverageIgnoreStart
158159
$this->writerBackend = new TempFileGZIPWriter($filePath);
160+
// @codeCoverageIgnoreEnd
159161
}
160162
} else {
161163
$this->writerBackend = new PlainFileWriter($filePath);
@@ -566,7 +568,9 @@ public function setUseIndent($value)
566568
public function setUseGzip($value)
567569
{
568570
if ($value && !extension_loaded('zlib')) {
571+
// @codeCoverageIgnoreStart
569572
throw new \RuntimeException('Zlib extension must be enabled to gzip the sitemap.');
573+
// @codeCoverageIgnoreEnd
570574
}
571575
if ($this->writerBackend !== null && $value != $this->useGzip) {
572576
throw new \RuntimeException('Cannot change the gzip value once items have been added to the sitemap.');

TempFileGZIPWriter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace samdark\sitemap;
44

5+
// @codeCoverageIgnoreStart
56
/**
6-
* Flushes buffer into temporary stream and compresses stream into a file on finish
7+
* Flushes buffer into temporary stream and compresses stream into a file on finish.
8+
*
9+
* Used on PHP builds where the zlib extension is available but incremental deflate functions are not.
710
*/
811
class TempFileGZIPWriter implements WriterInterface
912
{
@@ -13,7 +16,7 @@ class TempFileGZIPWriter implements WriterInterface
1316
private $filename;
1417

1518
/**
16-
* @var ressource for php://temp stream
19+
* @var resource for php://temp stream
1720
*/
1821
private $tempFile;
1922

@@ -54,3 +57,4 @@ public function finish()
5457
$this->tempFile = null;
5558
}
5659
}
60+
// @codeCoverageIgnoreEnd

UrlEncoderTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ protected function encodeUrl($url)
4545
$host = idn_to_ascii($parsed['host'], IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
4646
$encoded .= $host !== false ? $host : $parsed['host'];
4747
} else {
48+
// @codeCoverageIgnoreStart
4849
$encoded .= $parsed['host'];
50+
// @codeCoverageIgnoreEnd
4951
}
5052
}
5153

phpunit.xml.dist

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<phpunit bootstrap="./vendor/autoload.php">
3+
<coverage>
4+
<include>
5+
<file>./DeflateWriter.php</file>
6+
<file>./Index.php</file>
7+
<file>./PlainFileWriter.php</file>
8+
<file>./Sitemap.php</file>
9+
<file>./TempFileGZIPWriter.php</file>
10+
<file>./UrlEncoderTrait.php</file>
11+
<file>./WriterInterface.php</file>
12+
</include>
13+
</coverage>
314
<testsuites>
415
<testsuite name="Sitemap test suite">
516
<directory>./tests</directory>
617
</testsuite>
718
</testsuites>
8-
</phpunit>
19+
</phpunit>

tests/IndexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testLocationValidation()
3131

3232
$fileName = __DIR__ . '/sitemap.xml';
3333
$index = new Index($fileName);
34-
$index->addSitemap('noturl');
34+
$index->addSitemap('http://example.com:bad');
3535

3636
unlink($fileName);
3737
}

tests/SitemapTest.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ public function testMultipleFiles()
126126
unlink($expectedFile);
127127
}
128128

129+
$this->assertEquals($expectedFiles, $sitemap->getWrittenFilePath());
130+
129131
$urls = $sitemap->getSitemapUrls('http://example.com/');
130132
$this->assertEquals(10, count($urls), print_r($urls, true));
131133
$this->assertContains('http://example.com/sitemap_multi.xml', $urls);
@@ -206,6 +208,41 @@ public function testFrequencyValidation()
206208
unlink($fileName);
207209
}
208210

211+
public function testInvalidDirectoryValidation()
212+
{
213+
$this->expectException('InvalidArgumentException');
214+
215+
new Sitemap(__DIR__ . '/missing-directory/sitemap.xml');
216+
}
217+
218+
public function testExistingUnwritableFileValidation()
219+
{
220+
$fileName = __DIR__ . '/sitemap_unwritable.xml';
221+
file_put_contents($fileName, 'previous sitemap contents');
222+
chmod($fileName, 0444);
223+
224+
if (is_writable($fileName)) {
225+
chmod($fileName, 0644);
226+
unlink($fileName);
227+
$this->markTestSkipped('Filesystem does not make the file unwritable with chmod(0444).');
228+
}
229+
230+
$exceptionCaught = false;
231+
try {
232+
$sitemap = new Sitemap($fileName);
233+
$sitemap->addItem('http://example.com/mylink1');
234+
} catch (\RuntimeException $e) {
235+
$exceptionCaught = true;
236+
} finally {
237+
if (file_exists($fileName)) {
238+
chmod($fileName, 0644);
239+
unlink($fileName);
240+
}
241+
}
242+
243+
$this->assertTrue($exceptionCaught, 'Expected RuntimeException wasn\'t thrown.');
244+
}
245+
209246
public function testPriorityValidation()
210247
{
211248
$fileName = __DIR__ . '/sitemap.xml';
@@ -268,6 +305,52 @@ public function testMultiLanguageLocationValidation()
268305
$this->assertTrue($exceptionCaught, 'Expected InvalidArgumentException wasn\'t thrown.');
269306
}
270307

308+
public function testMultiLanguageFrequencyValidation()
309+
{
310+
$fileName = __DIR__ . '/sitemap.xml';
311+
$sitemap = new Sitemap($fileName, true);
312+
313+
$exceptionCaught = false;
314+
try {
315+
$sitemap->addItem(array(
316+
'de' => 'http://example.com/de/mylink1',
317+
'en' => 'http://example.com/en/mylink1',
318+
), time(), 'invalid');
319+
} catch (\InvalidArgumentException $e) {
320+
$exceptionCaught = true;
321+
}
322+
323+
unset($sitemap);
324+
if (file_exists($fileName)) {
325+
unlink($fileName);
326+
}
327+
328+
$this->assertTrue($exceptionCaught, 'Expected InvalidArgumentException wasn\'t thrown.');
329+
}
330+
331+
public function testMultiLanguagePriorityValidation()
332+
{
333+
$fileName = __DIR__ . '/sitemap.xml';
334+
$sitemap = new Sitemap($fileName, true);
335+
336+
$exceptionCaught = false;
337+
try {
338+
$sitemap->addItem(array(
339+
'de' => 'http://example.com/de/mylink1',
340+
'en' => 'http://example.com/en/mylink1',
341+
), time(), Sitemap::DAILY, 2.0);
342+
} catch (\InvalidArgumentException $e) {
343+
$exceptionCaught = true;
344+
}
345+
346+
unset($sitemap);
347+
if (file_exists($fileName)) {
348+
unlink($fileName);
349+
}
350+
351+
$this->assertTrue($exceptionCaught, 'Expected InvalidArgumentException wasn\'t thrown.');
352+
}
353+
271354
public function testWritingFileGzipped()
272355
{
273356
$fileName = __DIR__ . '/sitemap_gzipped.xml.gz';
@@ -379,6 +462,48 @@ public function testSmallSizeLimit()
379462
$this->assertTrue($exceptionCaught, 'Expected OverflowException wasn\'t thrown.');
380463
}
381464

465+
public function testWritingFileWithoutIndent()
466+
{
467+
$fileName = __DIR__ . '/sitemap_no_indent.xml';
468+
$sitemap = new Sitemap($fileName);
469+
$sitemap->setUseIndent(false);
470+
$sitemap->addItem('http://example.com/mylink1', 100, Sitemap::DAILY, 0.5);
471+
$sitemap->write();
472+
473+
$this->assertFileExists($fileName);
474+
$content = trim(file_get_contents($fileName));
475+
$expected = '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
476+
. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n"
477+
. '<url><loc>http://example.com/mylink1</loc>'
478+
. '<lastmod>1970-01-01T00:01:40+00:00</lastmod>'
479+
. '<changefreq>daily</changefreq>'
480+
. '<priority>0.5</priority></url></urlset>';
481+
482+
$this->assertSame($expected, $content);
483+
$this->assertIsValidSitemap($fileName);
484+
485+
unlink($fileName);
486+
}
487+
488+
public function testChangingGzipAfterWritingItemsIsRejected()
489+
{
490+
$fileName = __DIR__ . '/sitemap.xml';
491+
$sitemap = new Sitemap($fileName);
492+
$sitemap->addItem('http://example.com/mylink1');
493+
494+
$exceptionCaught = false;
495+
try {
496+
$sitemap->setUseGzip(true);
497+
} catch (\RuntimeException $e) {
498+
$exceptionCaught = true;
499+
}
500+
501+
unset($sitemap);
502+
unlink($fileName);
503+
504+
$this->assertTrue($exceptionCaught, 'Expected RuntimeException wasn\'t thrown.');
505+
}
506+
382507
public function testBufferSizeImpact()
383508
{
384509
if (getenv('TRAVIS') == 'true') {
@@ -707,4 +832,22 @@ public function testInternationalUrlEncoding()
707832
$this->assertIsValidSitemap($fileName);
708833
unlink($fileName);
709834
}
835+
836+
public function testComplexApplicationUrlEncoding()
837+
{
838+
$fileName = __DIR__ . '/sitemap_complex_url.xml';
839+
$sitemap = new Sitemap($fileName);
840+
$sitemap->addItem('http://user:secret@example.com:8080/search/кафе?tag=новости&preview#главная');
841+
$sitemap->write();
842+
843+
$this->assertFileExists($fileName);
844+
$content = file_get_contents($fileName);
845+
$this->assertStringContainsString(
846+
'http://user:secret@example.com:8080/search/%D0%BA%D0%B0%D1%84%D0%B5?tag=%D0%BD%D0%BE%D0%B2%D0%BE%D1%81%D1%82%D0%B8&amp;preview#%D0%B3%D0%BB%D0%B0%D0%B2%D0%BD%D0%B0%D1%8F',
847+
$content
848+
);
849+
850+
$this->assertIsValidSitemap($fileName);
851+
unlink($fileName);
852+
}
710853
}

tests/xhtml1-strict.xsd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
</xs:annotation>
3131

3232
<xs:import namespace="http://www.w3.org/XML/1998/namespace"
33-
schemaLocation="http://www.w3.org/2001/xml.xsd"/>
33+
schemaLocation="xml.xsd"/>
3434

3535
<xs:annotation>
3636
<xs:documentation>

tests/xml.xsd

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xs:schema targetNamespace="http://www.w3.org/XML/1998/namespace"
3+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
4+
xmlns:xml="http://www.w3.org/XML/1998/namespace"
5+
elementFormDefault="qualified"
6+
attributeFormDefault="qualified">
7+
<xs:attribute name="lang" type="xs:language"/>
8+
<xs:attribute name="space">
9+
<xs:simpleType>
10+
<xs:restriction base="xs:NCName">
11+
<xs:enumeration value="default"/>
12+
<xs:enumeration value="preserve"/>
13+
</xs:restriction>
14+
</xs:simpleType>
15+
</xs:attribute>
16+
<xs:attribute name="base" type="xs:anyURI"/>
17+
<xs:attribute name="id" type="xs:ID"/>
18+
</xs:schema>

0 commit comments

Comments
 (0)