Passed
Push — fix/out-of-memory-error-parser... ( 537936...6217ab )
by Konrad
02:30
created

ParserTest::testIssue334()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
c 1
b 1
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()
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()
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()
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()
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
     * Test that issue related pdf can now be parsed
172
     *
173
     * @see https://github.com/smalot/pdfparser/issues/267
174
     */
175
    public function testIssue267()
176
    {
177
        $filename = $this->rootDir.'/samples/bugs/Issue267_array_access_on_int.pdf';
178
179
        $document = $this->fixture->parseFile($filename);
180
181
        $this->assertEquals(Image::class, \get_class($document->getObjectById('128_0')));
182
        $this->assertStringContainsString('4 von 4', $document->getText());
183
    }
184
185
    /**
186
     * Test that issue related pdf can now be parsed:
187
     * Too many slashes were being stripped and resulted
188
     * in malformed encoding of parts of the text content.
189
     *
190
     * @see https://github.com/smalot/pdfparser/issues/322
191
     */
192
    public function testIssue322()
193
    {
194
        $filename = $this->rootDir.'/samples/bugs/Issue322.pdf';
195
196
        $document = $this->fixture->parseFile($filename);
197
198
        $this->assertStringContainsString('this text isn’t working properly, I’ve edited it in Google Documents', $document->getText());
199
    }
200
201
    /**
202
     * Test that issue related pdf can now be parsed:
203
     * Too many slashes were being stripped and resulted
204
     * in malformed encoding of parts of the text content.
205
     *
206
     * License of the content taken from https://stackoverflow.com in the sample PDF:
207
     * CC BY-SA 2.5 https://creativecommons.org/licenses/by-sa/2.5/
208
     *
209
     * @see https://github.com/smalot/pdfparser/issues/334
210
     */
211
    public function testIssue334()
212
    {
213
        $filename = $this->rootDir.'/samples/bugs/Issue334.pdf';
214
215
        $document = $this->fixture->parseFile($filename);
216
217
        $this->assertStringContainsString('This question already has an answer here', $document->getText());
218
    }
219
220
    /**
221
     * Test that issue related pdf can now be parsed:
222
     * Glyphs not in the Postscript lookup table would cause "Notice: Undefined offset"
223
     *
224
     * @see https://github.com/smalot/pdfparser/issues/359
225
     */
226
    public function testIssue359()
227
    {
228
        $filename = $this->rootDir.'/samples/bugs/Issue359.pdf';
229
230
        $document = $this->fixture->parseFile($filename);
231
232
        $this->assertStringContainsString(
233
            'dnia 10 maja 2018 roku o ochronie danych osobowych',
234
            $document->getText()
235
        );
236
        $this->assertStringContainsString('sprawie ochrony osób fizycznych w związku', $document->getText());
237
        /*
238
         * @todo Note that the "ł" in przepływu is decoded as a space character. This was already
239
         * the case before the PR that caused this issue and is not currently covered by this
240
         * test case. However, this issue should be addressed in the future and its fix can then
241
         * be incorporated into this test by uncommenting the following assertion.
242
         */
243
        // $this->assertStringContainsString('sprawie swobodnego przepływu takich danych oraz uchylenia dyrektywy', $document->getText());
244
    }
245
246
    /**
247
     * Tests if PDF triggers "Call to undefined method Smalot\PdfParser\Header::__toString()".
248
     *
249
     * It happened because there was a check missing in Font.php (~ line 109).
250
     *
251
     * @see https://github.com/smalot/pdfparser/issues/391
252
     */
253
    public function testIssue391()
254
    {
255
        /**
256
         * PDF provided by @dhildreth for usage in our test environment.
257
         *
258
         * @see https://github.com/smalot/pdfparser/issues/391#issuecomment-783504599
259
         */
260
        $filename = $this->rootDir.'/samples/bugs/Issue391.pdf';
261
262
        $document = $this->fixture->parseFile($filename);
263
264
        // check for an example string (PDF consists of many pages)
265
        $this->assertStringContainsString(
266
            '(This Code will be changed while mass production)',
267
            $document->getText()
268
        );
269
    }
270
271
    /**
272
     * Tests behavior when changing default font space limit (-50).
273
     *
274
     * Test is based on testIssue359 (above).
275
     */
276
    public function testChangedFontSpaceLimit()
277
    {
278
        $filename = $this->rootDir.'/samples/bugs/Issue359.pdf';
279
280
        $config = new Config();
281
        $config->setFontSpaceLimit(1); // change default value
282
283
        $this->fixture = new Parser([], $config);
284
        $document = $this->fixture->parseFile($filename);
285
286
        $this->assertStringContainsString('dni a  10  maj a  2018', $document->getText());
287
    }
288
289
    /**
290
     * Tests if a given Config object is really used.
291
     * Or if a default one is generated, if null was given.
292
     */
293
    public function testUsageOfConfigObject()
294
    {
295
        // check default
296
        $this->fixture = new Parser([]);
297
        $this->assertEquals(new Config(), $this->fixture->getConfig());
298
299
        // check default 2
300
        $this->fixture = new Parser([], null);
301
        $this->assertEquals(new Config(), $this->fixture->getConfig());
302
303
        // check given
304
        $config = new Config();
305
        $config->setFontSpaceLimit(1000);
306
        $this->fixture = new Parser([], $config);
307
        $this->assertEquals($config, $this->fixture->getConfig());
308
    }
309
310
    /**
311
     * Tests the impact of the retainImageContent config setting on memory usage
312
     *
313
     * @group memory-heavy
314
     *
315
     * @see https://github.com/smalot/pdfparser/issues/104#issuecomment-883422508
316
     */
317
    public function testRetainImageContentImpact()
318
    {
319
        if (version_compare(\PHP_VERSION, '7.3.0', '<')) {
320
            $this->markTestSkipped('Garbage collection doesn\'t work reliably enough for this test in PHP < 7.3');
321
        }
322
323
        $filename = $this->rootDir.'/samples/bugs/Issue104a.pdf';
324
        $iterations = 1;
325
326
        /*
327
         * check default (= true)
328
         */
329
        $this->fixture = new Parser([]);
330
        $this->assertTrue($this->fixture->getConfig()->getRetainImageContent());
331
        $document = null;
332
333
        for ($i = 0; $i < $iterations; ++$i) {
334
            $document = $this->fixture->parseFile($filename);
335
        }
336
337
        $this->assertTrue(memory_get_usage(true) > 100000000, 'Memory is only '.memory_get_usage());
338
        $this->assertTrue(null != $document && 0 < \strlen($document->getText()));
339
340
        // force garbage collection
341
        $this->fixture = $document = null;
342
        gc_collect_cycles();
343
344
        /*
345
         * check false
346
         */
347
        $config = new Config();
348
        $config->setRetainImageContent(false);
349
        $this->fixture = new Parser([], $config);
350
        $this->assertEquals($config, $this->fixture->getConfig());
351
352
        for ($i = 0; $i < $iterations; ++$i) {
353
            $document = $this->fixture->parseFile($filename);
354
        }
355
356
        $this->assertTrue(memory_get_usage(true) < 100000000);
357
        $this->assertTrue(0 < \strlen($document->getText()));
358
    }
359
}
360
361
class ParserSub extends Parser
362
{
363
    public function exposedParseObject($id, $structure, $document)
364
    {
365
        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...
366
    }
367
368
    public function getObjects()
369
    {
370
        return $this->objects;
371
    }
372
}
373