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-02 |
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\Element; |
34
|
|
|
|
35
|
|
|
use Smalot\PdfParser\Element\ElementString; |
36
|
|
|
use Tests\Smalot\PdfParser\TestCase; |
37
|
|
|
|
38
|
|
|
class ElementStringTest extends TestCase |
39
|
|
|
{ |
40
|
|
|
public function testParse() |
41
|
|
|
{ |
42
|
|
|
// Skipped. |
43
|
|
|
$offset = 0; |
44
|
|
|
$element = ElementString::parse('ABC', null, $offset); |
45
|
|
|
$this->assertFalse($element); |
46
|
|
|
$this->assertEquals(0, $offset); |
47
|
|
|
|
48
|
|
|
$offset = 0; |
49
|
|
|
$element = ElementString::parse(' [ (ABC) 5 6 ]', null, $offset); |
50
|
|
|
$this->assertFalse($element); |
51
|
|
|
$this->assertEquals(0, $offset); |
52
|
|
|
|
53
|
|
|
$offset = 0; |
54
|
|
|
$element = ElementString::parse(' << (invalid) >>', null, $offset); |
55
|
|
|
$this->assertFalse($element); |
56
|
|
|
$this->assertEquals(0, $offset); |
57
|
|
|
|
58
|
|
|
$offset = 0; |
59
|
|
|
$element = ElementString::parse(' / (FlateDecode) ', null, $offset); |
60
|
|
|
$this->assertFalse($element); |
61
|
|
|
$this->assertEquals(0, $offset); |
62
|
|
|
|
63
|
|
|
$offset = 0; |
64
|
|
|
$element = ElementString::parse(' 0 (FlateDecode) ', null, $offset); |
65
|
|
|
$this->assertFalse($element); |
66
|
|
|
$this->assertEquals(0, $offset); |
67
|
|
|
|
68
|
|
|
$offset = 0; |
69
|
|
|
$element = ElementString::parse(" 0 \n (FlateDecode) ", null, $offset); |
70
|
|
|
$this->assertFalse($element); |
71
|
|
|
$this->assertEquals(0, $offset); |
72
|
|
|
|
73
|
|
|
// Valid. |
74
|
|
|
$offset = 0; |
75
|
|
|
$element = ElementString::parse(' (Copyright) ', null, $offset); |
76
|
|
|
$this->assertEquals('Copyright', $element->getContent()); |
77
|
|
|
$this->assertEquals(12, $offset); |
78
|
|
|
|
79
|
|
|
$offset = 0; |
80
|
|
|
$element = ElementString::parse(' (Copyright) ', null, $offset); |
81
|
|
|
$this->assertEquals('Copyright', $element->getContent()); |
82
|
|
|
$this->assertEquals(12, $offset); |
83
|
|
|
|
84
|
|
|
$offset = 0; |
85
|
|
|
$element = ElementString::parse(' (Copyright)', null, $offset); |
86
|
|
|
$this->assertEquals('Copyright', $element->getContent()); |
87
|
|
|
$this->assertEquals(12, $offset); |
88
|
|
|
|
89
|
|
|
$offset = 0; |
90
|
|
|
$element = ElementString::parse('(Copyright)', null, $offset); |
91
|
|
|
$this->assertEquals('Copyright', $element->getContent()); |
92
|
|
|
$this->assertEquals(11, $offset); |
93
|
|
|
|
94
|
|
|
$offset = 0; |
95
|
|
|
$element = ElementString::parse('(Copy-right2)', null, $offset); |
96
|
|
|
$this->assertEquals('Copy-right2', $element->getContent()); |
97
|
|
|
$this->assertEquals(13, $offset); |
98
|
|
|
|
99
|
|
|
$offset = 0; |
100
|
|
|
$element = ElementString::parse(" \n (Copyright) ", null, $offset); |
101
|
|
|
$this->assertEquals('Copyright', $element->getContent()); |
102
|
|
|
$this->assertEquals(14, $offset); |
103
|
|
|
|
104
|
|
|
$offset = 0; |
105
|
|
|
$element = ElementString::parse('()', null, $offset); |
106
|
|
|
$this->assertEquals('', $element->getContent()); |
107
|
|
|
$this->assertEquals(2, $offset); |
108
|
|
|
|
109
|
|
|
/* |
110
|
|
|
* Complex study case : Unicode + octal. |
111
|
|
|
*/ |
112
|
|
|
$offset = 0; |
113
|
|
|
$element = ElementString::parse('(ABC\\))', null, $offset); |
114
|
|
|
$this->assertEquals('ABC)', $element->getContent()); |
115
|
|
|
$this->assertEquals(7, $offset); |
116
|
|
|
|
117
|
|
|
$offset = 0; |
118
|
|
|
$element = ElementString::parse("(\xFE\xFF\\000M)", null, $offset); |
119
|
|
|
$this->assertEquals('M', $element->getContent()); |
120
|
|
|
$this->assertEquals(9, $offset); |
121
|
|
|
|
122
|
|
|
$offset = 0; |
123
|
|
|
$element = ElementString::parse('(<20>)', null, $offset); |
124
|
|
|
$this->assertEquals(' ', $element->getContent()); |
125
|
|
|
$this->assertEquals(6, $offset); |
126
|
|
|
|
127
|
|
|
$offset = 0; |
128
|
|
|
$element = ElementString::parse('(Gutter\\ console\\ assembly)', null, $offset); |
129
|
|
|
$this->assertEquals('Gutter console assembly', $element->getContent()); |
130
|
|
|
$this->assertEquals(27, $offset); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testGetContent() |
134
|
|
|
{ |
135
|
|
|
$element = new ElementString('Copyright'); |
136
|
|
|
$this->assertEquals('Copyright', $element->getContent()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testEquals() |
140
|
|
|
{ |
141
|
|
|
$element = new ElementString('CopyRight'); |
142
|
|
|
$this->assertTrue($element->equals('CopyRight')); |
143
|
|
|
$this->assertFalse($element->equals('Flatedecode')); |
144
|
|
|
|
145
|
|
|
$element = new ElementString('CopyRight2'); |
146
|
|
|
$this->assertTrue($element->equals('CopyRight2')); |
147
|
|
|
$this->assertFalse($element->equals('CopyRight3')); |
148
|
|
|
|
149
|
|
|
$element = new ElementString('Flate-Decode2'); |
150
|
|
|
$this->assertTrue($element->equals('Flate-Decode2')); |
151
|
|
|
$this->assertFalse($element->equals('Flate-Decode3')); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testContains() |
155
|
|
|
{ |
156
|
|
|
$element = new ElementString('CopyRight'); |
157
|
|
|
$this->assertTrue($element->contains('CopyRight')); |
158
|
|
|
$this->assertFalse($element->contains('Copyright')); |
159
|
|
|
|
160
|
|
|
$element = new ElementString('CopyRight2'); |
161
|
|
|
$this->assertTrue($element->contains('CopyRight2')); |
162
|
|
|
$this->assertFalse($element->contains('CopyRight3')); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testToString() |
166
|
|
|
{ |
167
|
|
|
$element = new ElementString('CopyRight'); |
168
|
|
|
$this->assertEquals('CopyRight', (string) $element); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|