aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/mikespub/php-epub-meta/test/epubTest.php
blob: 5056fc260c61846ad146c8bcaf4a4dd914b7ce23 (plain) (blame)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
<?php

use PHPUnit\Framework\TestCase;
use SebLucas\EPubMeta\Contents\NavPoint as TocNavPoint;
use SebLucas\EPubMeta\EPub;
use SebLucas\EPubMeta\Data\Item as DataItem;
use SebLucas\EPubMeta\Tools\ZipEdit;

/**
 * Test for EPUB library
 *
 * Source: https://github.com/splitbrain/php-epub-meta
 * @author Andreas Gohr <andi@splitbrain.org> © 2012
 * @author Simon Schrape <simon@epubli.com> © 2015
 */
class EPubTest extends TestCase
{
    public const TEST_EPUB = __DIR__ . '/data/test.epub';
    public const TEST_EPUB_COPY = __DIR__ . '/data/test.copy.epub';
    public const TEST_EPUB_COVER = __DIR__ . '/data/test.cover.epub';
    public const TEST_IMAGE = __DIR__ . '/data/test.jpg';
    public const EMPTY_ZIP = __DIR__ . '/data/empty.zip';
    public const BROKEN_ZIP = __DIR__ . '/data/broken.zip';
    public const MARKUP_XML_1 = __DIR__ . '/data/markup.1.xml';
    public const MARKUP_XML_2 = __DIR__ . '/data/markup.2.xml';
    public const MARKUP_XML_3 = __DIR__ . '/data/markup.3.xml';
    public const MARKUP_XML_4 = __DIR__ . '/data/markup.4.xml';
    public const MARKUP_XML_5 = __DIR__ . '/data/markup.5.xml';

    protected EPub $epub;

    protected function setUp(): void
    {
        // sometime I might have accidentally broken the test file
        if (filesize(static::TEST_EPUB) != 768780) {
            die('test.epub has wrong size, make sure it\'s unmodified');
        }

        // we work on a copy to test saving
        if (!copy(static::TEST_EPUB, static::TEST_EPUB_COPY)) {
            die('failed to create copy of the test book');
        }

        // @see https://github.com/sebastianbergmann/phpunit/issues/5062#issuecomment-1416362657
        set_error_handler(
            static function (int $errno, string $errstr) {
                throw new \Exception($errstr, $errno);
            },
            E_ALL
        );

        $this->epub = new Epub(static::TEST_EPUB_COPY);
    }

    protected function tearDown(): void
    {
        restore_error_handler();

        unlink(static::TEST_EPUB_COPY);
    }

    public function testGetZipEntries(): void
    {
        $entries = $this->epub->getZipEntries();
        $this->assertCount(49, $entries);
        $this->assertArrayHasKey(EPub::METADATA_FILE, $entries);
        $this->assertEquals(250, $entries[EPub::METADATA_FILE]['size']);

        $count = $this->epub->getImageCount();
        $this->assertEquals(3, $count);

        $coverpath = $this->epub->getCoverPath();
        $this->assertEquals('images/cover.png', $coverpath);

        $size = $this->epub->getComponentSize($coverpath);
        $this->assertEquals(657911, $size);
    }

    public function testLoadNonZip(): void
    {
        //$this->expectException(Exception::class);
        //$this->expectExceptionMessage('Failed to read EPUB file. Not a zip archive.');
        //$this->expectExceptionMessage('Failed to read epub file');
        $this->expectException(ValueError::class);
        $this->expectExceptionMessage('Invalid or uninitialized Zip object');
        new Epub(static::TEST_IMAGE);
    }

    public function testLoadBrokenZip(): void
    {
        //$this->expectException(Exception::class);
        //$this->expectExceptionMessage('Failed to read EPUB file. Zip archive inconsistent.');
        //$this->expectExceptionMessage('Unable to find metadata.xml');
        $this->expectException(ValueError::class);
        $this->expectExceptionMessage('Invalid or uninitialized Zip object');
        new Epub(static::BROKEN_ZIP);
    }

    public function testLoadMissingFile(): void
    {
        $this->expectException(Exception::class);
        //$this->expectExceptionMessage('Failed to read EPUB file. No such file.');
        $this->expectExceptionMessage('Epub file does not exist!');
        new Epub('/a/file/that/is/not_there.epub');
    }

    /**
     * We cannot expect a more specific exception message. ZipArchive::open returns 28
     * which is not known as an error code.
     */
    public function testLoadDirectory(): void
    {
        $this->expectException(Exception::class);
        //$this->expectExceptionMessage('Failed to read EPUB file.');
        $this->expectExceptionMessage('Epub file does not exist!');
        new Epub(__DIR__);
    }

    public function testLoadEmptyZip(): void
    {
        $this->expectException(Exception::class);
        //$this->expectExceptionMessage('Failed to read from EPUB container: META-INF/container.xml');
        //$this->expectExceptionMessage('Failed to read epub file');
        $this->expectExceptionMessage('Unable to find ' . EPub::METADATA_FILE);
        new Epub(static::EMPTY_ZIP);
    }

    public function testFilename(): void
    {
        $this->assertEquals(static::TEST_EPUB_COPY, $this->epub->file());
    }

    public function testAuthors(): void
    {
        // read curent value
        $this->assertEquals(['Shakespeare, William' => 'William Shakespeare'], $this->epub->getAuthors());

        // remove value with string
        $this->epub->setAuthors('');
        $this->assertEquals([], $this->epub->getAuthors());

        // set single value by String
        $this->epub->setAuthors('John Doe');
        $this->assertEquals(['John Doe' => 'John Doe'], $this->epub->getAuthors());

        // set single value by indexed array
        $this->epub->setAuthors(['John Doe']);
        $this->assertEquals(['John Doe' => 'John Doe'], $this->epub->getAuthors());

        // remove value with array
        $this->epub->setAuthors([]);
        $this->assertEquals([], $this->epub->getAuthors());

        // set single value by associative array
        $this->epub->setAuthors(['Doe, John' => 'John Doe']);
        $this->assertEquals(['Doe, John' => 'John Doe'], $this->epub->getAuthors());

        // set multi value by string
        $this->epub->setAuthors('John Doe, Jane Smith');
        $this->assertEquals(['John Doe' => 'John Doe', 'Jane Smith' => 'Jane Smith'], $this->epub->getAuthors());

        // set multi value by indexed array
        $this->epub->setAuthors(['John Doe', 'Jane Smith']);
        $this->assertEquals(['John Doe' => 'John Doe', 'Jane Smith' => 'Jane Smith'], $this->epub->getAuthors());

        // set multi value by associative  array
        $this->epub->setAuthors(['Doe, John' => 'John Doe', 'Smith, Jane' => 'Jane Smith']);
        $this->assertEquals(['Doe, John' => 'John Doe', 'Smith, Jane' => 'Jane Smith'], $this->epub->getAuthors());

        // check escaping
        $this->epub->setAuthors(['Doe, John&nbsp;' => 'John Doe&nbsp;']);
        $this->assertEquals(['Doe, John&nbsp;' => 'John Doe&nbsp;'], $this->epub->getAuthors());
    }

    public function testTitle(): void
    {
        // get current value
        $this->assertEquals('Romeo and Juliet', $this->epub->getTitle());

        // set new value
        $this->epub->setTitle('Foo Bar');
        $this->assertEquals('Foo Bar', $this->epub->getTitle());

        // delete current value
        $this->epub->setTitle('');
        $this->assertEquals('', $this->epub->getTitle());

        // check escaping
        $this->epub->setTitle('Foo&nbsp;Bar');
        $this->assertEquals('Foo&nbsp;Bar', $this->epub->getTitle());
    }

    public function testLanguage(): void
    {
        // get current value
        $this->assertEquals('en', $this->epub->getLanguage());

        // set new value
        $this->epub->setLanguage('Foo Bar');
        $this->assertEquals('Foo Bar', $this->epub->getLanguage());

        // delete current value
        $this->epub->setLanguage('');
        $this->assertEquals('', $this->epub->getLanguage());

        // check escaping
        $this->epub->setLanguage('Foo&nbsp;Bar');
        $this->assertEquals('Foo&nbsp;Bar', $this->epub->getLanguage());
    }

    public function testPublisher(): void
    {
        // get current value
        $this->assertEquals('Feedbooks', $this->epub->getPublisher());

        // set new value
        $this->epub->setPublisher('Foo Bar');
        $this->assertEquals('Foo Bar', $this->epub->getPublisher());

        // delete current value
        $this->epub->setPublisher('');
        $this->assertEquals('', $this->epub->getPublisher());

        // check escaping
        $this->epub->setPublisher('Foo&nbsp;Bar');
        $this->assertEquals('Foo&nbsp;Bar', $this->epub->getPublisher());
    }

    public function testCopyright(): void
    {
        // get current value
        $this->assertEquals('', $this->epub->getCopyright());

        // set new value
        $this->epub->setCopyright('Foo Bar');
        $this->assertEquals('Foo Bar', $this->epub->getCopyright());

        // delete current value
        $this->epub->setCopyright('');
        $this->assertEquals('', $this->epub->getCopyright());

        // check escaping
        $this->epub->setCopyright('Foo&nbsp;Bar');
        $this->assertEquals('Foo&nbsp;Bar', $this->epub->getCopyright());
    }

    public function testDescription(): void
    {
        // get current value
        $this->assertStringStartsWith('Romeo and Juliet is a tragic play written', $this->epub->getDescription());

        // set new value
        $this->epub->setDescription('Foo Bar');
        $this->assertEquals('Foo Bar', $this->epub->getDescription());

        // delete current value
        $this->epub->setDescription('');
        $this->assertEquals('', $this->epub->getDescription());

        // check escaping
        $this->epub->setDescription('Foo&nbsp;Bar');
        $this->assertEquals('Foo&nbsp;Bar', $this->epub->getDescription());
    }

    public function testUniqueIdentifier(): void
    {
        // get current value
        $this->assertEquals('urn:uuid:7d38d098-4234-11e1-97b6-001cc0a62c0b', $this->epub->getUniqueIdentifier());

        // set new value
        $this->epub->setUniqueIdentifier('134htb34tp089h1b');
        $this->assertEquals('134htb34tp089h1b', $this->epub->getUniqueIdentifier());
        // this should have affected the same node that is found when looking for UUID/URN scheme
        $this->assertEquals('134htb34tp089h1b', $this->epub->getUuid());
    }

    public function testUuid(): void
    {
        // get current value
        $this->assertEquals('urn:uuid:7d38d098-4234-11e1-97b6-001cc0a62c0b', $this->epub->getUuid());

        // set new value
        $this->epub->setUuid('Foo Bar');
        $this->assertEquals('Foo Bar', $this->epub->getUuid());

        // delete current value
        $this->epub->setUuid('');
        $this->assertEquals('', $this->epub->getUuid());

        // check escaping
        $this->epub->setUuid('Foo&nbsp;Bar');
        $this->assertEquals('Foo&nbsp;Bar', $this->epub->getUuid());
    }

    public function testUri(): void
    {
        // get current value
        $this->assertEquals('http://www.feedbooks.com/book/2936', $this->epub->getUri());

        // set new value
        $this->epub->setUri('Foo Bar');
        $this->assertEquals('Foo Bar', $this->epub->getUri());

        // delete current value
        $this->epub->setUri('');
        $this->assertEquals('', $this->epub->getUri());

        // check escaping
        $this->epub->setUri('Foo&nbsp;Bar');
        $this->assertEquals('Foo&nbsp;Bar', $this->epub->getUri());
    }

    public function testIsbn(): void
    {
        // get current value
        $this->assertEquals('', $this->epub->getIsbn());

        // set new value
        $this->epub->setIsbn('Foo Bar');
        $this->assertEquals('Foo Bar', $this->epub->getIsbn());

        // delete current value
        $this->epub->setIsbn('');
        $this->assertEquals('', $this->epub->getIsbn());

        // check escaping
        $this->epub->setIsbn('Foo&nbsp;Bar');
        $this->assertEquals('Foo&nbsp;Bar', $this->epub->getIsbn());
    }

    public function testSubject(): void
    {
        // get current values
        $this->assertEquals(['Fiction', 'Drama', 'Romance'], $this->epub->getSubjects());

        // delete current values with String
        $this->epub->setSubjects('');
        $this->assertEquals([], $this->epub->getSubjects());

        // set new values with String
        $this->epub->setSubjects('Fiction, Drama, Romance');
        $this->assertEquals(['Fiction', 'Drama', 'Romance'], $this->epub->getSubjects());

        // delete current values with Array
        $this->epub->setSubjects([]);
        $this->assertEquals([], $this->epub->getSubjects());

        // set new values with array
        $this->epub->setSubjects(['Fiction', 'Drama', 'Romance']);
        $this->assertEquals(['Fiction', 'Drama', 'Romance'], $this->epub->getSubjects());

        // check escaping
        $this->epub->setSubjects(['Fiction', 'Drama&nbsp;', 'Romance']);
        $this->assertEquals(['Fiction', 'Drama&nbsp;', 'Romance'], $this->epub->getSubjects());
    }

    public function testCover(): void
    {
        // we work on a copy to test saving
        $this->assertTrue(copy(static::TEST_EPUB, static::TEST_EPUB_COVER));

        // use the ZipEdit class here
        //$epub = new EPub(static::TEST_EPUB_COVER, ZipEdit::class);
        $epub = new EPub(static::TEST_EPUB_COVER);

        // read current cover
        $cover = $epub->getCover();
        $this->assertEquals(657911, strlen($cover));

        // change cover and save
        $epub->setCover(static::TEST_IMAGE, 'image/jpeg');
        //$epub->save();

        // open epub again
        //$epub = new EPub(static::TEST_EPUB_COVER);

        // read recently changed cover
        $cover = $epub->getCover();
        $this->assertEquals(filesize(static::TEST_IMAGE), strlen($cover));

        // delete cover
        $epub->clearCover();
        $cover = $epub->getCover();
        $this->assertNull($cover);

        $epub->close();

        unlink(static::TEST_EPUB_COVER);
    }

    /**
     * @throws Exception
     * @return void
     */
    public function testTitlePage()
    {
        // we work on a copy to test saving
        $this->assertTrue(copy(static::TEST_EPUB, static::TEST_EPUB_COVER));

        // use the ZipEdit class here
        //$epub = new EPub(static::TEST_EPUB_COVER, ZipEdit::class);
        $epub = new EPub(static::TEST_EPUB_COVER);

        // add title page and save
        $epub->addCoverImageTitlePage();
        //$epub->save();

        // open epub again
        //$epub = new EPub(static::TEST_EPUB_COVER);

        // read recently added title page
        $spine = $epub->getSpine();
        $titlePage = $spine->first();
        $this->assertEquals(EPub::TITLE_PAGE_ID . '.xhtml', $titlePage->getHref());
        $this->assertEquals(EPub::TITLE_PAGE_ID, $titlePage->getId());
        $this->assertEquals('application/xhtml+xml', (string) $titlePage->getMediaType());

        // We expect an empty string since there is only an image but no text on that page.
        $this->assertEmpty(trim($titlePage->getContents()));

        unlink(static::TEST_EPUB_COVER);
    }

    /**
     * @throws Exception
     * @return void
     */
    public function testCalibreAnnotations()
    {
        $epub = new Epub(static::TEST_EPUB);
        // use metadata from .opf file for tests here - see Alice from mikespub/seblucas-cops
        $data = file_get_contents(__DIR__ . '/data/metadata.opf');
        $annotations = $epub->getCalibreAnnotations($data);
        $this->assertCount(5, $annotations);
        $expected = [
            'format' => 'EPUB',
            'user_type' => 'local',
            'user' => 'viewer',
            'annotation' => [
                'title' => 'About #1',
                'pos_type' => 'epubcfi',
                'pos' => 'epubcfi(/6/2/4/2/6/2:38)',
                'timestamp' => '2024-03-11T11:54:35.128396+00:00',
                'type' => 'bookmark',
            ],
        ];
        $this->assertEquals($expected, $annotations[0]);
    }

    /**
     * @throws Exception
     * @return void
     */
    public function testCalibreBookmarks()
    {
        $epub = new Epub(static::TEST_EPUB);
        // use calibre_bookmarks.txt for tests here - see Alice from mikespub/seblucas-cops
        $data = file_get_contents(__DIR__ . '/data/calibre_bookmarks.txt');
        $bookmarks = $epub->getCalibreBookmarks($data);
        $this->assertCount(5, $bookmarks);
        $expected = [
            'title' => 'About #1',
            'pos_type' => 'epubcfi',
            'pos' => 'epubcfi(/6/2/4/2/6/2:38)',
            'timestamp' => '2024-03-11T11:54:35.128396+00:00',
            'type' => 'bookmark',
        ];
        $this->assertEquals($expected, $bookmarks[0]);
    }

    /**
     * @throws Exception
     * @return void
     */
    public function testManifest()
    {
        $manifest = $this->epub->getManifest();
        $this->assertCount(41, $manifest);

        $this->assertEquals('cover', $manifest->first()->getId());
        $this->assertEquals(DataItem::XHTML, $manifest->current()->getMediaType());
        $manifest->next();
        $this->assertEquals('title.xml', $manifest->current()->getHref());
        $this->assertEquals('ncx', $manifest->last()->getId());

        $this->assertSame($manifest['cover'], $manifest->first());
        $this->assertSame($manifest['ncx'], $manifest->last());
    }

    /**
     * @throws Exception
     * @return void
     */
    public function testSpine()
    {
        $spine = $this->epub->getSpine();
        $this->assertCount(31, $spine);

        $this->assertEquals('cover', $spine->first()->getId());
        $this->assertEquals(DataItem::XHTML, $spine->current()->getMediaType());
        $spine->next();
        $this->assertEquals('title.xml', $spine->current()->getHref());
        $this->assertEquals('feedbooks', $spine->last()->getId());

        $this->assertEquals('fb.ncx', $spine->getTocItem()->getHref());

        $this->assertSame($spine[0], $spine->first());
        $this->assertSame($spine[30], $spine->last());
    }

    /**
     * @throws Exception
     * @return void
     */
    public function testToc()
    {
        $this->assertEquals(2, $this->epub->getEpubVersion());

        $toc = $this->epub->getToc();
        $this->assertEquals('Romeo and Juliet', $toc->getDocTitle());
        $this->assertEquals('Shakespeare, William', $toc->getDocAuthor());
        $navMap = $toc->getNavMap();
        $this->assertEquals(8, $navMap->count());

        $navPoint = $navMap->first();
        /** @var TocNavPoint $navPoint */
        $this->assertEquals('level1-titlepage', $navPoint->getId());
        $this->assertEquals('titlepage', $navPoint->getClass());
        $this->assertEquals('1', $navPoint->getPlayOrder());
        $this->assertEquals('Title', $navPoint->getNavLabel());
        $this->assertEquals('title.xml', $navPoint->getContentSource());
        $this->assertCount(0, $navPoint->getChildren());

        $navMap->next();
        $navMap->next();
        $navPoint = $navMap->current();
        /** @var TocNavPoint $navPoint */
        $this->assertEquals('sec77303', $navPoint->getId());
        $this->assertEquals('section', $navPoint->getClass());
        $this->assertEquals('3', $navPoint->getPlayOrder());
        $this->assertEquals('Act I', $navPoint->getNavLabel());
        $this->assertEquals('main0.xml', $navPoint->getContentSource());
        $this->assertCount(6, $navPoint->getChildren());
        $this->assertEquals('Prologue', $navPoint->getChildren()->first()->getNavLabel());
        $this->assertEquals('SCENE V. A hall in Capulet\'s house.', $navPoint->getChildren()->last()->getNavLabel());
    }

    /**
     * @throws Exception
     * @return void
     */
    public function testNav()
    {
        $test_epub3 = __DIR__ . '/data/nav-non-text_img_title.epub';
        $test_epub3_copy = __DIR__ . '/data/nav-non-text_img_title.copy.epub';

        // sometime I might have accidentally broken the test file
        $this->assertEquals(239564, filesize($test_epub3));

        // we work on a copy to test saving
        $this->assertTrue(copy($test_epub3, $test_epub3_copy));

        $epub = new EPub($test_epub3_copy);
        $this->assertEquals(3, $epub->getEpubVersion());

        $toc = $epub->getNav();
        $this->assertEquals('nav-non-text_img_title', $toc->getDocTitle());
        $this->assertEquals('Ivan Herman', $toc->getDocAuthor());
        $navMap = $toc->getNavMap();
        $this->assertEquals(2, $navMap->count());

        $navPoint = $navMap->first();
        /** @var TocNavPoint $navPoint */
        $this->assertEquals('', $navPoint->getId());
        $this->assertEquals('h1', $navPoint->getClass());
        $this->assertEquals('1', $navPoint->getPlayOrder());
        $this->assertEquals('Start page', $navPoint->getNavLabel());
        $this->assertEquals('content_001.xhtml', $navPoint->getContentSource());
        $this->assertCount(0, $navPoint->getChildren());

        $navMap->next();
        $navPoint = $navMap->current();
        /** @var TocNavPoint $navPoint */
        $this->assertEquals('', $navPoint->getId());
        $this->assertEquals('h1', $navPoint->getClass());
        $this->assertEquals('2', $navPoint->getPlayOrder());
        $this->assertEquals('Description of the Abbey of Sénanque', $navPoint->getNavLabel());
        $this->assertEquals('senanque.xhtml', $navPoint->getContentSource());
        $this->assertCount(0, $navPoint->getChildren());
        //$this->assertEquals('Prologue', $navPoint->getChildren()->first()->getNavLabel());
        //$this->assertEquals('SCENE V. A hall in Capulet\'s house.', $navPoint->getChildren()->last()->getNavLabel());

        unlink($test_epub3_copy);
    }

    /**
     * @throws Exception
     * @return void
     */
    public function testNavTree()
    {
        $test_epub3 = __DIR__ . '/data/eng3.epub';
        $test_epub3_copy = __DIR__ . '/data/eng3.copy.epub';

        // sometime I might have accidentally broken the test file
        $this->assertEquals(53216, filesize($test_epub3));

        // we work on a copy to test saving
        $this->assertTrue(copy($test_epub3, $test_epub3_copy));

        $epub = new EPub($test_epub3_copy);

        $toc = $epub->getNav();
        $this->assertEquals('Calibre Quick Start Guide', $toc->getDocTitle());
        $this->assertEquals('John Schember', $toc->getDocAuthor());
        $navMap = $toc->getNavMap();
        $this->assertEquals(7, $navMap->count());
        $this->assertCount(1, $navMap->findNavPointsForFile('text/introduction.xhtml'));
        $this->assertCount(0, $navMap->findNavPointsForFile('oops/are_we_lost?.xhtml'));

        $navPoint = $navMap->first();
        /** @var TocNavPoint $navPoint */
        $this->assertEquals('', $navPoint->getId());
        $this->assertEquals('h1', $navPoint->getClass());
        $this->assertEquals('1', $navPoint->getPlayOrder());
        $this->assertEquals('Calibre Quick Start Guide', $navPoint->getNavLabel());
        $this->assertEquals('text/internal_titlepage.xhtml', $navPoint->getContentSource());
        $this->assertCount(0, $navPoint->getChildren());

        $navMap->seek(5);
        $navPoint = $navMap->current();
        /** @var TocNavPoint $navPoint */
        $this->assertEquals('', $navPoint->getId());
        $this->assertEquals('h1', $navPoint->getClass());
        $this->assertEquals(6, $navPoint->getPlayOrder());
        $this->assertEquals('Common Tasks', $navPoint->getNavLabel());
        $this->assertEquals('text/common_tasks.xhtml', $navPoint->getContentSource());
        $this->assertCount(6, $navPoint->getChildren());
        $this->assertEquals('Task 1: Organizing', $navPoint->getChildren()->first()->getNavLabel());
        $navPoint->getChildren()->next();
        $childPoint = $navPoint->getChildren()->current();
        $this->assertEquals('Task 2: Conversion', $childPoint->getNavLabel());
        $this->assertCount(7, $childPoint->getChildren());
        $this->assertEquals('Task 6: The e-book viewer', $navPoint->getChildren()->last()->getNavLabel());

        unlink($test_epub3_copy);
    }

    /**
     * @dataProvider provideContentsTestParameters
     * @param string $referenceStart The expected start of the extracted contents.
     * @param string $referenceEnd The expected end of the extracted contents.
     * @param int $referenceSize The expected size of the extracted contents.
     * @param bool $keepMarkup Whether to extract contents with or without HTML markup.
     * @param float $fraction
     * @throws Exception
     * @return void
     */
    public function testContents(
        $referenceStart,
        $referenceEnd,
        $referenceSize,
        $keepMarkup,
        $fraction
    ) {
        $contents = trim($this->epub->getContents($keepMarkup, $fraction));
        $this->assertStringStartsWith($referenceStart, substr($contents, 0, 100));
        $this->assertStringEndsWith($referenceEnd, substr($contents, -100));
        $this->assertEquals($referenceSize, strlen($contents));
    }

    /**
     * Summary of provideContentsTestParameters
     * @return array<mixed>
     */
    public static function provideContentsTestParameters()
    {
        return [
            ["Romeo and Juliet\n\nWilliam Shakespeare", "www.feedbooks.com\n\n    Food for the mind", 152879, false, 1],
            ["Romeo and Juliet\n\nWilliam Shakespeare", "seek happy nights to happy days.\n\nExeunt", 24936, false, .2],
            ["Romeo and Juliet\n\nWilliam Shakespeare", "miss, our toil shall strive to mend.", 3810, false, .1],
        ];
    }

    /**
     * @dataProvider provideItemContentsTestParameters
     * @param string $referenceStart The expected start of the extracted contents.
     * @param string $referenceEnd The expected end of the extracted contents.
     * @param string $spineIndex The spine index of the item to extract contents from.
     * @param string $fragmentBegin The anchor name (ID) where to start extraction.
     * @param string $fragmentEnd The anchor name (ID) where to end extraction.
     * @throws Exception
     * @return void
     */
    public function testItemContents(
        $referenceStart,
        $referenceEnd,
        $spineIndex,
        $fragmentBegin = null,
        $fragmentEnd = null
    ) {
        $spine = $this->epub->getSpine();
        $contents = trim($spine[$spineIndex]->getContents($fragmentBegin, $fragmentEnd));
        $this->assertStringStartsWith($referenceStart, $contents);
        $this->assertStringEndsWith($referenceEnd, $contents);
    }

    /**
     * Summary of provideItemContentsTestParameters
     * @return array<mixed>
     */
    public static function provideItemContentsTestParameters()
    {
        return [
            ['Act I', 'our toil shall strive to mend.', 3],
            ['SCENE I. Verona. A public place.', "I'll pay that doctrine, or else die in debt.\n\nExeunt", 4],
            ['Act III', 'Act III', 16, 'section_77331', 'section_77332'],
            ['Act III', 'Act III', 16, null, 'section_77332'],
            ['SCENE I. A public place.', "pardoning those that kill.\n\nExeunt", 16, 'section_77332'],
        ];
    }

    public function testItemContentsStartFragmentException(): void
    {
        $this->expectException(Exception::class);
        $this->expectExceptionMessage('Begin of fragment not found:');
        $spine = $this->epub->getSpine();
        $spine[3]->getContents('NonExistingElement');
    }

    public function testItemContentsEndFragmentException(): void
    {
        $this->expectException(Exception::class);
        $this->expectExceptionMessage('End of fragment not found:');
        $spine = $this->epub->getSpine();
        $spine[3]->getContents(null, 'NonExistingElement');
    }

    /**
     * @dataProvider provideItemContentsMarkupTestParameters
     * @param string $referenceFile
     * @param string $spineIndex
     * @param string $fragmentBegin
     * @param string $fragmentEnd
     * @throws Exception
     * @return void
     */
    public function testItemContentsMarkup($referenceFile, $spineIndex, $fragmentBegin = null, $fragmentEnd = null)
    {
        $spine = $this->epub->getSpine();
        $contents = $spine[$spineIndex]->getContents($fragmentBegin, $fragmentEnd, true);
        $contents = preg_replace('/\s+/m', ' ', $contents);
        $extracted = new DOMDocument();
        $extracted->loadXML($contents);
        $extstring = $extracted->saveXML($extracted->documentElement);
        $extstring = preg_replace('/\s*([<>])\s*/m', '$1', $extstring);
        $reference = new DOMDocument();
        $contents = file_get_contents($referenceFile);
        $contents = preg_replace('/\s+/m', ' ', $contents);
        $reference->loadXML($contents);
        $refstring = $reference->saveXML($reference->documentElement);
        $refstring = preg_replace('/\s*([<>])\s*/m', '$1', $refstring);
        $this->assertEquals($refstring, $extstring);
        //$this->assertEqualXMLStructure($reference->documentElement, $extracted->documentElement);
    }

    /**
     * Summary of provideItemContentsMarkupTestParameters
     * @return array<mixed>
     */
    public static function provideItemContentsMarkupTestParameters()
    {
        return [
            [static::MARKUP_XML_1, 3],
            [static::MARKUP_XML_2, 4],
            [static::MARKUP_XML_3, 16, 'section_77331', 'section_77332'],
            [static::MARKUP_XML_4, 16, null, 'section_77332'],
            [static::MARKUP_XML_5, 16, 'section_77332'],
        ];
    }

    /**
     * @throws Exception
     * @return void
     */
    public function testItemDataSize()
    {
        $item = $this->epub->getSpine()[0];
        $size = $item->getSize();
        $data = $item->getData();

        $this->assertEquals(strlen($data), $size);
    }

    /**
     * Summary of testZipEdit
     * @return void
     */
    public function testZipEdit()
    {
        ZipEdit::copyTest(static::TEST_EPUB_COPY, static::TEST_EPUB_COVER);
        $epub = new EPub(static::TEST_EPUB_COVER);
        $oldManifest = $this->epub->getManifest();
        $newManifest = $epub->getManifest();
        $this->assertEquals($oldManifest, $newManifest);
    }
}