@@ -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&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}
0 commit comments