Passed
Pull Request — master (#560)
by
unknown
03:32
created

ParserTest::testIssue19()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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