Passed
Pull Request — master (#510)
by Jeremy
06:50 queued 03:58
created

ParserTest::testEbook()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * @file This file is part of the PdfParser library.
5
 *
6
 * @author  Konrad Abicht <[email protected]>
7
 * @date    2020-06-01
8
 *
9
 * @author  Sébastien MALOT <[email protected]>
10
 * @date    2017-01-03
11
 *
12
 * @license LGPLv3
13
 * @url     <https://github.com/smalot/pdfparser>
14
 *
15
 *  PdfParser is a pdf library written in PHP, extraction oriented.
16
 *  Copyright (C) 2017 - Sébastien MALOT <[email protected]>
17
 *
18
 *  This program is free software: you can redistribute it and/or modify
19
 *  it under the terms of the GNU Lesser General Public License as published by
20
 *  the Free Software Foundation, either version 3 of the License, or
21
 *  (at your option) any later version.
22
 *
23
 *  This program is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 *  GNU Lesser General Public License for more details.
27
 *
28
 *  You should have received a copy of the GNU Lesser General Public License
29
 *  along with this program.
30
 *  If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
31
 */
32
33
namespace Tests\Smalot\PdfParser\Integration;
34
35
use Exception;
36
use Smalot\PdfParser\Config;
37
use Smalot\PdfParser\Document;
38
use Smalot\PdfParser\Parser;
39
use Smalot\PdfParser\XObject\Image;
40
use Tests\Smalot\PdfParser\TestCase;
41
42
class ParserTest extends TestCase
43
{
44
    protected function setUp(): void
45
    {
46
        parent::setUp();
47
48
        $this->fixture = new Parser();
49
    }
50
51
    /**
52
     * Notice: it may fail to run in Scrutinizer because of memory limitations.
53
     *
54
     * @group memory-heavy
55
     */
56
    public function testParseFile(): void
57
    {
58
        $directory = $this->rootDir.'/samples/bugs';
59
60
        if (is_dir($directory)) {
61
            $files = scandir($directory);
62
63
            foreach ($files as $file) {
64
                if (preg_match('/^.*\.pdf$/i', $file)) {
65
                    try {
66
                        $document = $this->fixture->parseFile($directory.'/'.$file);
67
                        $pages = $document->getPages();
68
                        $this->assertTrue(0 < \count($pages));
69
70
                        foreach ($pages as $page) {
71
                            $content = $page->getText();
72
                            $this->assertTrue(0 < \strlen($content));
73
                        }
74
                    } catch (Exception $e) {
75
                        if (
76
                            'Secured pdf file are currently not supported.' !== $e->getMessage()
77
                            && 0 != strpos($e->getMessage(), 'TCPDF_PARSER')
78
                        ) {
79
                            throw $e;
80
                        }
81
                    }
82
                }
83
            }
84
        }
85
    }
86
87
    /**
88
     * Properly decode international unicode characters
89
     *
90
     * @todo the other languages in the test document need work because of issues with UTF-16 decoding (Chinese, Japanese) and missing right-to-left language support
91
     */
92
    public function testUnicodeDecoding(): void
93
    {
94
        $filename = $this->rootDir.'/samples/InternationalChars.pdf';
95
96
        $document = $this->fixture->parseFile($filename);
97
98
        $testString_cyrillic = "Лорем ипсум долор сит амет, еу сед либрис долорем инцоррупте. Ут лорем долоре граеце хис, модо \nаппареат сапиентем ут мел. Хис ат лаборе омнесяуе сигниферумяуе, тале анциллае ан еум, ех сед синт \nнобис. Сед модус вивендо цопиосае еа, сапиентем цонцептам хис не, яуандо сплендиде еум те.";
99
        $testString_greek = "Λορεμ ιπσθμ δολορ σιτ αμετ, τατιον cονστιτθαμ ομιτταντθρ εα σεα, αθδιαμ μανδαμθσ μελ τε. Διcο μθτατ \nινδοcτθμ εοσ ει, ει vιξ σονετ παρτιενδο ινcορρθπτε. Επιcθρι αντιοπαμ εθ νεc, ναμ λεγιμθσ γθβεργρεν ιν. \nVιξ σολετ ρεcτεqθε εα, ηασ νο αλιqθαμ μινιμθμ. Ιδ προ περcιπιτ περιcθλισ δετερρθισσετ, ιν νεc αππετερε \nομιτταντθρ ελοqθεντιαμ, ορατιο δοcτθσ ναμ αδ. Ετ σιτ σολθμ ρεcθσαβο, vιξ θτ λοβορτισ σπλενδιδε \nρεπθδιανδαε.";
100
        $testString_armenian = "լոռեմ իպսում դոլոռ սիթ ամեթ վիս ին իմպեդիթ ադմոդում ծու ապպառեաթ սծռիպսեռիթ մել մել եթ \nդոմինգ ծոնսեքուունթուռ ծիվիբուս վիվենդում պռոդեսսեթ ադ մեի թիբիքուե ապպառեաթ սիմիլիքուե թե \nվիմ վիխ ծասե սեմպեռ դոլոռեմ եխ եամ եա սթեթ մեդիոծռեմ ծոնսեթեթուռ ռաթիոնիբուս ինթելլեգամ \nմել թե";
101
        $testString_georgean = "ლორემ იფსუმ დოლორ სით ამეთ ესთ ეთ სონეთ ზრილ მელიუს ელიგენდი თორყუათოს \nელოყუენთიამ ესთ ეხ უსუ ფალლი ალთერა ცეთეროს ინ ეთ ომითთამ თრაცთათოს ჰის ეუ ველ \nალთერუმ ვოლუფთათუმ მაზიმ ფერთინახ ჰენდრერით ინ ფრი ნეც ინ თემფორ ფეთენთიუმ ვერო \nფოსთულანთ ელოყუენთიამ უსუ ნე ან ყუი ლიბერ ეფიცური ასსუევერით იდ ნიბჰ ყუას ჰაბემუს სეა";
102
        $testString_korean = "그 임기는 4년으로 하며. 이 경우 그 명령에 의하여 개정 또는 폐지되었던 법률은 그 명령이 승인을 얻지 못한 때부터 당연히 효력을 \n회복한다. 가부동수인 때에는 부결된 것으로 본다. 법률과 적법한 절차에 의하지 아니하고는 처벌·보안처분 또는 강제노역을 받지 \n아니한다.";
103
        $testString_western = 'ÄÖÜöäüßẞ Ññ¡¿ øÅå';
104
105
        $this->assertStringContainsString($testString_cyrillic, $document->getText());
106
        $this->assertStringContainsString($testString_greek, $document->getText());
107
        $this->assertStringContainsString($testString_armenian, $document->getText());
108
        $this->assertStringContainsString($testString_georgean, $document->getText());
109
        $this->assertStringContainsString($testString_korean, $document->getText());
110
        $this->assertStringContainsString($testString_western, $document->getText());
111
    }
112
113
    /**
114
     * Tests that xrefs with line breaks between id and position are parsed correctly
115
     *
116
     * @see https://github.com/smalot/pdfparser/issues/336
117
     */
118
    public function testIssue19(): void
119
    {
120
        $fixture = new ParserSub();
121
        $structure = [
122
            [
123
                '<<',
124
                [
125
                    [
126
                        '/',
127
                        'Type',
128
                        7735,
129
                    ],
130
                    [
131
                        '/',
132
                        'ObjStm',
133
                        7742,
134
                    ],
135
                ],
136
            ],
137
            [
138
                'stream',
139
                '',
140
                7804,
141
                [
142
                    "17\n0",
143
                    [],
144
                ],
145
            ],
146
        ];
147
        $document = new Document();
148
149
        $fixture->exposedParseObject('19_0', $structure, $document);
150
        $objects = $fixture->getObjects();
151
152
        $this->assertArrayHasKey('17_0', $objects);
153
    }
154
155
    /**
156
     * Properly decode ANSI encodings without producing scrambled UTF-8 characters
157
     *
158
     * @see https://github.com/smalot/pdfparser/issues/202
159
     * @see https://github.com/smalot/pdfparser/pull/257
160
     */
161
    public function testIssue202(): void
162
    {
163
        $filename = $this->rootDir.'/samples/bugs/Issue202.pdf';
164
165
        $document = $this->fixture->parseFile($filename);
166
167
        $this->assertEquals('„fööbär“', $document->getText());
168
    }
169
170
    /**
171
     * Without a proper fix it throws deprecated message like:
172
     *
173
     * `Invalid characters passed for attempted conversion, these have been ignored`
174
     */
175
    public function testEbook(): void
176
    {
177
        $filename = $this->rootDir.'/samples/ebook_sept2003.pdf';
178
179
        $document = $this->fixture->parseFile($filename);
180
181
        $this->assertEquals('Ebook3-6-version-pdf.sxw', $document->getDetails()['Title']);
182
    }
183
184
    /**
185
     * Test that issue related pdf can now be parsed
186
     *
187
     * @see https://github.com/smalot/pdfparser/issues/267
188
     */
189
    public function testIssue267(): void
190
    {
191
        $filename = $this->rootDir.'/samples/bugs/Issue267_array_access_on_int.pdf';
192
193
        $document = $this->fixture->parseFile($filename);
194
195
        $this->assertEquals(Image::class, \get_class($document->getObjectById('128_0')));
196
        $this->assertStringContainsString('4 von 4', $document->getText());
197
    }
198
199
    /**
200
     * Test that issue related pdf can now be parsed:
201
     * Too many slashes were being stripped and resulted
202
     * in malformed encoding of parts of the text content.
203
     *
204
     * @see https://github.com/smalot/pdfparser/issues/322
205
     */
206
    public function testIssue322(): void
207
    {
208
        $filename = $this->rootDir.'/samples/bugs/Issue322.pdf';
209
210
        $document = $this->fixture->parseFile($filename);
211
212
        $this->assertStringContainsString('this text isn’t working properly, I’ve edited it in Google Documents', $document->getText());
213
    }
214
215
    /**
216
     * Test that issue related pdf can now be parsed:
217
     * Too many slashes were being stripped and resulted
218
     * in malformed encoding of parts of the text content.
219
     *
220
     * License of the content taken from https://stackoverflow.com in the sample PDF:
221
     * CC BY-SA 2.5 https://creativecommons.org/licenses/by-sa/2.5/
222
     *
223
     * @see https://github.com/smalot/pdfparser/issues/334
224
     */
225
    public function testIssue334(): void
226
    {
227
        $filename = $this->rootDir.'/samples/bugs/Issue334.pdf';
228
229
        $document = $this->fixture->parseFile($filename);
230
231
        $this->assertStringContainsString('This question already has an answer here', $document->getText());
232
    }
233
234
    /**
235
     * Test that issue related pdf can now be parsed:
236
     * Glyphs not in the Postscript lookup table would cause "Notice: Undefined offset"
237
     *
238
     * @see https://github.com/smalot/pdfparser/issues/359
239
     */
240
    public function testIssue359(): void
241
    {
242
        $filename = $this->rootDir.'/samples/bugs/Issue359.pdf';
243
244
        $document = $this->fixture->parseFile($filename);
245
246
        $this->assertStringContainsString(
247
            'dnia 10 maja 2018 roku o ochronie danych osobowych',
248
            $document->getText()
249
        );
250
        $this->assertStringContainsString('sprawie ochrony osób fizycznych w związku', $document->getText());
251
        /*
252
         * @todo Note that the "ł" in przepływu is decoded as a space character. This was already
253
         * the case before the PR that caused this issue and is not currently covered by this
254
         * test case. However, this issue should be addressed in the future and its fix can then
255
         * be incorporated into this test by uncommenting the following assertion.
256
         */
257
        // $this->assertStringContainsString('sprawie swobodnego przepływu takich danych oraz uchylenia dyrektywy', $document->getText());
258
    }
259
260
    /**
261
     * Tests if PDF triggers "Call to undefined method Smalot\PdfParser\Header::__toString()".
262
     *
263
     * It happened because there was a check missing in Font.php (~ line 109).
264
     *
265
     * @see https://github.com/smalot/pdfparser/issues/391
266
     */
267
    public function testIssue391(): void
268
    {
269
        /**
270
         * PDF provided by @dhildreth for usage in our test environment.
271
         *
272
         * @see https://github.com/smalot/pdfparser/issues/391#issuecomment-783504599
273
         */
274
        $filename = $this->rootDir.'/samples/bugs/Issue391.pdf';
275
276
        $document = $this->fixture->parseFile($filename);
277
278
        // check for an example string (PDF consists of many pages)
279
        $this->assertStringContainsString(
280
            '(This Code will be changed while mass production)',
281
            $document->getText()
282
        );
283
    }
284
285
    /**
286
     * Tests behavior when changing default font space limit (-50).
287
     *
288
     * Test is based on testIssue359 (above).
289
     */
290
    public function testChangedFontSpaceLimit(): void
291
    {
292
        $filename = $this->rootDir.'/samples/bugs/Issue359.pdf';
293
294
        $config = new Config();
295
        $config->setFontSpaceLimit(1); // change default value
296
297
        $this->fixture = new Parser([], $config);
298
        $document = $this->fixture->parseFile($filename);
299
300
        $this->assertStringContainsString('dni a  10  maj a  2018', $document->getText());
301
    }
302
303
    /**
304
     * Tests if a given Config object is really used.
305
     * Or if a default one is generated, if null was given.
306
     */
307
    public function testUsageOfConfigObject(): void
308
    {
309
        // check default
310
        $this->fixture = new Parser([]);
311
        $this->assertEquals(new Config(), $this->fixture->getConfig());
312
313
        // check default 2
314
        $this->fixture = new Parser([], null);
315
        $this->assertEquals(new Config(), $this->fixture->getConfig());
316
317
        // check given
318
        $config = new Config();
319
        $config->setFontSpaceLimit(1000);
320
        $this->fixture = new Parser([], $config);
321
        $this->assertEquals($config, $this->fixture->getConfig());
322
    }
323
324
    /**
325
     * Tests the impact of the retainImageContent config setting on memory usage
326
     *
327
     * @group memory-heavy
328
     *
329
     * @see https://github.com/smalot/pdfparser/issues/104#issuecomment-883422508
330
     */
331
    public function testRetainImageContentImpact(): void
332
    {
333
        if (version_compare(\PHP_VERSION, '7.3.0', '<')) {
334
            $this->markTestSkipped('Garbage collection doesn\'t work reliably enough for this test in PHP < 7.3');
335
        }
336
337
        gc_collect_cycles();
338
        $baselineMemory = memory_get_usage(true);
339
340
        $filename = $this->rootDir.'/samples/bugs/Issue104a.pdf';
341
        $iterations = 2;
342
343
        /*
344
         * check default (= true)
345
         */
346
        $this->fixture = new Parser([]);
347
        $this->assertTrue($this->fixture->getConfig()->getRetainImageContent());
348
        $document = null;
349
350
        for ($i = 0; $i < $iterations; ++$i) {
351
            $document = $this->fixture->parseFile($filename);
352
        }
353
354
        $usedMemory = memory_get_usage(true);
355
        $this->assertTrue($usedMemory > ($baselineMemory * 1.5), 'Memory is only '.$usedMemory);
356
        $this->assertTrue(null != $document && 0 < \strlen($document->getText()));
357
358
        // force garbage collection
359
        $this->fixture = $document = null;
360
        gc_collect_cycles();
361
362
        /*
363
         * check false
364
         */
365
        $config = new Config();
366
        $config->setRetainImageContent(false);
367
        $this->fixture = new Parser([], $config);
368
        $this->assertEquals($config, $this->fixture->getConfig());
369
370
        for ($i = 0; $i < $iterations; ++$i) {
371
            $document = $this->fixture->parseFile($filename);
372
        }
373
374
        $usedMemory = memory_get_usage(true);
375
        /*
376
         * note: the following memory value is set manually and may differ from system to system.
377
         *       it must be high enough to not produce a false negative though.
378
         */
379
        $this->assertTrue($usedMemory < ($baselineMemory * 1.05), 'Memory is '.$usedMemory);
380
        $this->assertTrue(0 < \strlen($document->getText()));
381
    }
382
}
383
384
class ParserSub extends Parser
385
{
386
    public function exposedParseObject($id, $structure, $document)
387
    {
388
        return $this->parseObject($id, $structure, $document);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->parseObject($id, $structure, $document) targeting Smalot\PdfParser\Parser::parseObject() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
389
    }
390
391
    public function getObjects(): array
392
    {
393
        return $this->objects;
394
    }
395
}
396