Passed
Pull Request — master (#349)
by
unknown
01:53
created

ParserTest::testIssue267()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 4
c 2
b 1
f 0
nc 1
nop 0
dl 0
loc 8
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\Parser;
37
use Smalot\PdfParser\XObject\Image;
38
use Tests\Smalot\PdfParser\TestCase;
39
40
class ParserTest extends TestCase
41
{
42
    public function setUp()
43
    {
44
        parent::setUp();
45
46
        $this->fixture = new Parser();
47
    }
48
49
    public function testParseFile()
50
    {
51
        $directory = $this->rootDir.'/samples/bugs';
52
53
        if (is_dir($directory)) {
54
            $files = scandir($directory);
55
56
            foreach ($files as $file) {
57
                if (preg_match('/^.*\.pdf$/i', $file)) {
58
                    try {
59
                        $document = $this->fixture->parseFile($directory.'/'.$file);
60
                        $pages = $document->getPages();
61
                        $this->assertTrue(0 < \count($pages));
62
63
                        foreach ($pages as $page) {
64
                            $content = $page->getText();
65
                            $this->assertTrue(0 < \strlen($content));
66
                        }
67
                    } catch (Exception $e) {
68
                        if (
69
                            'Secured pdf file are currently not supported.' !== $e->getMessage()
70
                            && 0 != strpos($e->getMessage(), 'TCPDF_PARSER')
71
                        ) {
72
                            throw $e;
73
                        }
74
                    }
75
                }
76
            }
77
        }
78
    }
79
80
    /**
81
     * Properly decode international unicode characters
82
     *
83
     * @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
84
     */
85
    public function testUnicodeDecoding()
86
    {
87
        $filename = $this->rootDir.'/samples/InternationalChars.pdf';
88
89
        $document = $this->fixture->parseFile($filename);
90
91
        $testString_cyrillic = "Лорем ипсум долор сит амет, еу сед либрис долорем инцоррупте. Ут лорем долоре граеце хис, модо \nаппареат сапиентем ут мел. Хис ат лаборе омнесяуе сигниферумяуе, тале анциллае ан еум, ех сед синт \nнобис. Сед модус вивендо цопиосае еа, сапиентем цонцептам хис не, яуандо сплендиде еум те.";
92
        $testString_greek = "Λορεμ ιπσθμ δολορ σιτ αμετ, τατιον cονστιτθαμ ομιτταντθρ εα σεα, αθδιαμ μανδαμθσ μελ τε. Διcο μθτατ \nινδοcτθμ εοσ ει, ει vιξ σονετ παρτιενδο ινcορρθπτε. Επιcθρι αντιοπαμ εθ νεc, ναμ λεγιμθσ γθβεργρεν ιν. \nVιξ σολετ ρεcτεqθε εα, ηασ νο αλιqθαμ μινιμθμ. Ιδ προ περcιπιτ περιcθλισ δετερρθισσετ, ιν νεc αππετερε \nομιτταντθρ ελοqθεντιαμ, ορατιο δοcτθσ ναμ αδ. Ετ σιτ σολθμ ρεcθσαβο, vιξ θτ λοβορτισ σπλενδιδε \nρεπθδιανδαε.";
93
        $testString_armenian = "լոռեմ իպսում դոլոռ սիթ ամեթ վիս ին իմպեդիթ ադմոդում ծու ապպառեաթ սծռիպսեռիթ մել մել եթ \nդոմինգ ծոնսեքուունթուռ ծիվիբուս վիվենդում պռոդեսսեթ ադ մեի թիբիքուե ապպառեաթ սիմիլիքուե թե \nվիմ վիխ ծասե սեմպեռ դոլոռեմ եխ եամ եա սթեթ մեդիոծռեմ ծոնսեթեթուռ ռաթիոնիբուս ինթելլեգամ \nմել թե";
94
        $testString_georgean = "ლორემ იფსუმ დოლორ სით ამეთ ესთ ეთ სონეთ ზრილ მელიუს ელიგენდი თორყუათოს \nელოყუენთიამ ესთ ეხ უსუ ფალლი ალთერა ცეთეროს ინ ეთ ომითთამ თრაცთათოს ჰის ეუ ველ \nალთერუმ ვოლუფთათუმ მაზიმ ფერთინახ ჰენდრერით ინ ფრი ნეც ინ თემფორ ფეთენთიუმ ვერო \nფოსთულანთ ელოყუენთიამ უსუ ნე ან ყუი ლიბერ ეფიცური ასსუევერით იდ ნიბჰ ყუას ჰაბემუს სეა";
95
        $testString_korean = "그 임기는 4년으로 하며. 이 경우 그 명령에 의하여 개정 또는 폐지되었던 법률은 그 명령이 승인을 얻지 못한 때부터 당연히 효력을 \n회복한다. 가부동수인 때에는 부결된 것으로 본다. 법률과 적법한 절차에 의하지 아니하고는 처벌·보안처분 또는 강제노역을 받지 \n아니한다.";
96
        $testString_western = 'ÄÖÜöäüßẞ Ññ¡¿ øÅå';
97
98
        $this->assertStringContainsString($testString_cyrillic, $document->getText());
99
        $this->assertStringContainsString($testString_greek, $document->getText());
100
        $this->assertStringContainsString($testString_armenian, $document->getText());
101
        $this->assertStringContainsString($testString_georgean, $document->getText());
102
        $this->assertStringContainsString($testString_korean, $document->getText());
103
        $this->assertStringContainsString($testString_western, $document->getText());
104
    }
105
106
    /**
107
     * Properly decode ANSI encodings without producing scrambled UTF-8 characters
108
     *
109
     * @see https://github.com/smalot/pdfparser/issues/202
110
     * @see https://github.com/smalot/pdfparser/pull/257
111
     */
112
    public function testIssue202()
113
    {
114
        $filename = $this->rootDir.'/samples/bugs/Issue202.pdf';
115
116
        $document = $this->fixture->parseFile($filename);
117
118
        $this->assertEquals('„fööbär“', $document->getText());
119
    }
120
121
    /**
122
     * Test that issue related pdf can now be parsed
123
     *
124
     * @see https://github.com/smalot/pdfparser/issues/267
125
     */
126
    public function testIssue267()
127
    {
128
        $filename = $this->rootDir.'/samples/bugs/Issue267_array_access_on_int.pdf';
129
130
        $document = $this->fixture->parseFile($filename);
131
132
        $this->assertEquals(Image::class, \get_class($document->getObjectById('128_0')));
133
        $this->assertStringContainsString('4 von 4', $document->getText());
134
    }
135
136
    /**
137
     * Test that issue related pdf can now be parsed:
138
     * Too many slashes were being stripped and resulted
139
     * in malformed encoding of parts of the text content.
140
     *
141
     * @see https://github.com/smalot/pdfparser/issues/322
142
     */
143
    public function testIssue322()
144
    {
145
        $filename = $this->rootDir.'/samples/bugs/Issue322.pdf';
146
147
        $document = $this->fixture->parseFile($filename);
148
149
        $this->assertStringContainsString('this text isn’t working properly, I’ve edited it in Google Documents', $document->getText());
150
    }
151
152
    /**
153
     * Test that issue related pdf can now be parsed:
154
     * Too many slashes were being stripped and resulted
155
     * in malformed encoding of parts of the text content.
156
     *
157
     * License of the content taken from https://stackoverflow.com in the sample PDF:
158
     * CC BY-SA 2.5 https://creativecommons.org/licenses/by-sa/2.5/
159
     *
160
     * @see https://github.com/smalot/pdfparser/issues/334
161
     */
162
    public function testIssue334()
163
    {
164
        $filename = $this->rootDir.'/samples/bugs/Issue334.pdf';
165
166
        $document = $this->fixture->parseFile($filename);
167
168
        $this->assertStringContainsString('This question already has an answer here', $document->getText());
169
    }
170
}
171