1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of dispositif/wikibot application |
4
|
|
|
* 2019 : Philippe M. <[email protected]> |
5
|
|
|
* For the full copyright and MIT license information, please view the LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace App\Domain\Models\Wiki\Tests; |
11
|
|
|
|
12
|
|
|
use App\Domain\Models\Wiki\GoogleLivresTemplate; |
13
|
|
|
use Exception; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
|
16
|
|
|
class GoogleLivresTemplateTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
public function testIsGoogleBookURL() |
19
|
|
|
{ |
20
|
|
|
$url |
21
|
|
|
= 'https://books.google.fr/books?id=pbspjvZst5UC&pg=PA395&lpg=PA395&dq=D%C3%A9cret-Loi+10+septembre+1926&source=bl&ots=kiCzMrHO7b&sig=Jxt2Ybpig7Oo-Mtuzgp_sL5ipQ4&hl=fr&sa=X&ei=6SMLU_zIDarL0AX75YAI&ved=0CFEQ6AEwBA#v=onepage&q=D%C3%A9cret-Loi%2010%20septembre%201926&f=false'; |
22
|
|
|
$this::assertEquals( |
23
|
|
|
true, |
24
|
|
|
GoogleLivresTemplate::isGoogleBookURL($url) |
25
|
|
|
); |
26
|
|
|
$this::assertEquals( |
27
|
|
|
true, |
28
|
|
|
GoogleLivresTemplate::isGoogleBookValue($url) |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @dataProvider provideSimplify |
34
|
|
|
* |
35
|
|
|
* @param $url |
36
|
|
|
* @param $expected |
37
|
|
|
* |
38
|
|
|
* @throws Exception |
39
|
|
|
*/ |
40
|
|
|
public function testSimplyGoogleBookUrl($url, $expected) |
41
|
|
|
{ |
42
|
|
|
$this::assertEquals( |
43
|
|
|
$expected, |
44
|
|
|
GoogleLivresTemplate::simplifyGoogleUrl($url) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function provideSimplify() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
[ |
52
|
|
|
// .com.au |
53
|
|
|
'https://books.google.com.au/books?id=QHrQoDLNBUIC&pg=PT19&lpg=PT19&dq=Iotape+of+Commagene&source=web&ots=aZ3hKg3uDr&sig=Y_zdZhNP-qNZE6WIDNivPPm-Urg&hl=en&sa=X&oi=book_result&resnum=8&ct=result', |
54
|
|
|
'https://books.google.com.au/books?id=QHrQoDLNBUIC&pg=PT19&dq=Iotape+of+Commagene', |
55
|
|
|
], |
56
|
|
|
[ |
57
|
|
|
// 'id' in the middle |
58
|
|
|
'https://books.google.fr/books?hl=fr&id=CWkrAQAAMAAJ&dq=La+dur%C3%A9e+d%27ensoleillement+n%27est+pas+suffisante+en+Afrique&focus=searchwithinvolume&q=ceintures', |
59
|
|
|
'https://books.google.fr/books?id=CWkrAQAAMAAJ&q=ceintures', |
60
|
|
|
], |
61
|
|
|
[ |
62
|
|
|
// strange format |
63
|
|
|
'https://books.google.fr/books/about/Kate_Bush.html?id=YL0EDgAAQBAJ&printsec=frontcover&source=kp_read_button&redir_esc=y#v=onepage&q&f=false', |
64
|
|
|
'https://books.google.fr/books?id=YL0EDgAAQBAJ&printsec=frontcover', |
65
|
|
|
], |
66
|
|
|
[ |
67
|
|
|
// Maroc : sous-domaine .co.ma |
68
|
|
|
'https://books.google.co.ma/books?id=26gcP_Yz-i8C&PG=PA56', |
69
|
|
|
'https://books.google.co.ma/books?id=26gcP_Yz-i8C&pg=PA56', |
70
|
|
|
], |
71
|
|
|
[ |
72
|
|
|
// uppercase "ID=" |
73
|
|
|
'https://books.google.fr/books?ID=26gcP_Yz-i8C&PG=PA56', |
74
|
|
|
'https://books.google.fr/books?id=26gcP_Yz-i8C&pg=PA56', |
75
|
|
|
], |
76
|
|
|
[ |
77
|
|
|
// common pattern |
78
|
|
|
'https://books.google.fr/books?id=26gcP_Yz-i8C&pg=PA56&lpg=PA56&dq=André+Poznanski&source=bl&ots=tuFKKbkpUS&sig=ACfU3U058ij4qQHFsXX_KX01YK81SLCCBw&hl=fr&sa=X&ved=2ahUKEwiB6tHVtKbkAhULNRoKHbOeDXU4ChDoATAAegQICBAB#v=onepage&q=André%20Poznanski&f=false', |
79
|
|
|
'https://books.google.fr/books?id=26gcP_Yz-i8C&pg=PA56&dq=Andr%C3%A9+Poznanski' |
80
|
|
|
], |
81
|
|
|
[ |
82
|
|
|
// pattern 'http://' and '/books/reader' |
83
|
|
|
'http://books.google.com/books/reader?id=WH4rAAAAYAAJ', |
84
|
|
|
'https://books.google.com/books?id=WH4rAAAAYAAJ', |
85
|
|
|
], |
86
|
|
|
[ |
87
|
|
|
// pattern rare : https://books.google.com/?id=-0h134NR1s0C |
88
|
|
|
'https://books.google.com/?id=-0h134NR1s0C&pg=PA167&lpg=PA167&dq=Prairie+Shores+apartments+Michael+Reese#v=onepage&q=Prairie%20Shores%20apartments%20Michael%20Reese&f=false', |
89
|
|
|
'https://books.google.com/books?id=-0h134NR1s0C&pg=PA167&dq=Prairie+Shores+apartments+Michael+Reese', |
90
|
|
|
], |
91
|
|
|
[ |
92
|
|
|
// frontcover |
93
|
|
|
'https://books.google.fr/books?id=lcHcXrVhRUUC&printsec=frontcover&hl=fr&source=gbs_ge_summary_r&cad=0#v=onepage&q&f=false', |
94
|
|
|
'https://books.google.fr/books?id=lcHcXrVhRUUC&printsec=frontcover', |
95
|
|
|
], |
96
|
|
|
[ |
97
|
|
|
// play.google.com (rare) |
98
|
|
|
'https://play.google.com/books/reader?id=1dtkAAAAMAAJ&printsec=frontcover&output=reader&hl=fr&pg=GBS.PR7', |
99
|
|
|
'https://books.google.com/books?id=1dtkAAAAMAAJ&pg=GBS.PR7&printsec=frontcover', |
100
|
|
|
], |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testIsTrackingUrl() |
105
|
|
|
{ |
106
|
|
|
$url = 'https://books.google.com.au/books?id=QHrQoDLNBUIC&pg=PT19&lpg=PT19&dq=Iotape+of+Commagene&source=web&ots=aZ3hKg3uDr&sig=Y_zdZhNP-qNZE6WIDNivPPm-Urg&hl=en&sa=X&oi=book_result&resnum=8&ct=result'; |
107
|
|
|
$this::assertSame( |
108
|
|
|
true, |
109
|
|
|
GoogleLivresTemplate::isTrackingUrl($url) |
110
|
|
|
); |
111
|
|
|
$url = 'https://books.google.com.au/books?id=QHrQoDLNBUIC&pg=PT19'; |
112
|
|
|
$this::assertSame( |
113
|
|
|
false, |
114
|
|
|
GoogleLivresTemplate::isTrackingUrl($url) |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @dataProvider provideGoogleUrl |
120
|
|
|
* |
121
|
|
|
* @param string $url |
122
|
|
|
* @param string $expected |
123
|
|
|
* |
124
|
|
|
* @throws Exception |
125
|
|
|
*/ |
126
|
|
|
public function testCreateFromURL(string $url, string $expected) |
127
|
|
|
{ |
128
|
|
|
$this::assertEquals( |
129
|
|
|
$expected, |
130
|
|
|
GoogleLivresTemplate::createFromURL($url)->serialize() |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function provideGoogleUrl(): array |
135
|
|
|
{ |
136
|
|
|
return [ |
137
|
|
|
[ |
138
|
|
|
'https://books.google.fr/books?id=pbspjvZst5UC', |
139
|
|
|
'{{Google Livres|pbspjvZst5UC}}', |
140
|
|
|
], |
141
|
|
|
[ |
142
|
|
|
// partial book and cover |
143
|
|
|
'https://books.google.com/books?id=UNgxtsjOIf4C&printsec=frontcover', |
144
|
|
|
'{{Google Livres|UNgxtsjOIf4C|couv=1}}', |
145
|
|
|
], |
146
|
|
|
[ |
147
|
|
|
// page pg=PA... (arabe) |
148
|
|
|
'https://books.google.com/books?id=UNgxtsjOIf4C&pg=PA333', |
149
|
|
|
'{{Google Livres|UNgxtsjOIf4C|page=333}}', |
150
|
|
|
], |
151
|
|
|
[ |
152
|
|
|
// page pg=PR... (romain) |
153
|
|
|
'https://books.google.com/books?id=UNgxtsjOIf4C&pg=PR333', |
154
|
|
|
'{{Google Livres|UNgxtsjOIf4C|page=333|romain=1}}', |
155
|
|
|
], |
156
|
|
|
[ |
157
|
|
|
// page autre RAz-PAx |
158
|
|
|
'https://books.google.fr/books?id=BS4HAQAAIAAJ&pg=RA1-PA184', |
159
|
|
|
'{{Google Livres|BS4HAQAAIAAJ|page autre=RA1-PA184}}', |
160
|
|
|
], |
161
|
|
|
[ |
162
|
|
|
// page autre PTx |
163
|
|
|
'https://books.google.fr/books?id=YqZDAgAAQBAJ&pg=PT77', |
164
|
|
|
'{{Google Livres|YqZDAgAAQBAJ|page autre=PT77}}', |
165
|
|
|
], |
166
|
|
|
[ |
167
|
|
|
// surlignage |
168
|
|
|
'https://books.google.fr/books?id=pbspjvZst5UC&pg=PA395&lpg=PA395&dq=D%C3%A9cret-Loi+10+septembre+1926&source=bl&ots=kiCzMrHO7b&sig=Jxt2Ybpig7Oo-Mtuzgp_sL5ipQ4&hl=fr&sa=X&ei=6SMLU_zIDarL0AX75YAI&ved=0CFEQ6AEwBA#v=onepage&q=D%C3%A9cret-Loi%2010%20septembre%201926&f=false', |
169
|
|
|
'{{Google Livres|pbspjvZst5UC|page=395|surligne=Décret-Loi+10+septembre+1926}}', |
170
|
|
|
], |
171
|
|
|
]; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testCreateFromUrlWithTitle() |
175
|
|
|
{ |
176
|
|
|
$url = 'https://books.google.fr/books?id=pbspjvZst5UC'; |
177
|
|
|
$google = GoogleLivresTemplate::createFromURL($url); |
178
|
|
|
$google->setParam('titre', 'Hello'); |
179
|
|
|
|
180
|
|
|
$this::assertEquals( |
181
|
|
|
'{{Google Livres|pbspjvZst5UC|Hello}}', |
182
|
|
|
$google->serialize() |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|