PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents / 2.7.2
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents v2.7.2
4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.10 4.4.11 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5.0 4.5.1
embedpress / vendor / wpdevelopers / embera / tests / Embera / ProviderTester.php
embedpress / vendor / wpdevelopers / embera / tests / Embera Last commit date
Cache 5 years ago Html 5 years ago Http 5 years ago Provider 5 years ago ProviderCollection 5 years ago EmberaTest.php 5 years ago ProviderTester.php 5 years ago UrlTest.php 5 years ago
ProviderTester.php
324 lines
1 <?php
2 /**
3 * TestProviders.php
4 *
5 * @package Tests
6 * @author Michael Pratt <pratt@hablarmierda.net>
7 * @link http://www.michael-pratt.com/
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13 namespace Embera;
14
15 use Embera\Http\HttpClient;
16 use Embera\Http\OembedClient;
17 use Embera\ProviderCollection\DefaultProviderCollection;
18 use PHPUnit\Framework\TestCase;
19 use ReflectionClass;
20
21 class ProviderTester extends TestCase
22 {
23 /** @var array with urls */
24 protected $tasks;
25
26 /**
27 * Returns an array with random data from an array
28 *
29 * @param array $data
30 * @param int $count
31 *
32 * @return array
33 *
34 */
35 protected function getRandomDataFrom(array $data, $count = 1)
36 {
37 if ($count >= count($data)) {
38 return $data;
39 }
40
41 shuffle($data);
42 return array_slice($data, 0, $count);
43 }
44
45 /**
46 * This is where it gets nasty.
47 *
48 * This method is used to validate the behaviour of a Provider.
49 * All the other methods found down here, are used to test different
50 * parts of the provider
51 *
52 * Now, every provider has a test file which extends this class
53 * and uses this method to validate everything.
54 *
55 * Why? I had so much duplicated code...
56 * I know it looks ugly, but, its more convenient.
57 *
58 * @param string $p The provider Name
59 * @param array $config Additional config options
60 * @return void
61 *
62 * @large
63 */
64 protected function validateProvider($p, array $config = [])
65 {
66 $rounds = 1000;
67 $validateRealResponse = true;
68 if ((bool) getenv('TRAVIS')) {
69 $rounds = 1;
70 $validateRealResponse = false;
71 }
72
73 if (empty($this->tasks)) {
74 throw new \Exception(sprintf('The Provider %s doesnt have tasks', $p));
75 }
76
77 $this->validateUrlDetection($p, $this->tasks, $config);
78 $this->validateCollectionDetection($p, $this->tasks, $config);
79
80 if (!empty($this->tasks['normalize_urls'])) {
81 $this->validateUrlNormalization($p, $this->tasks['normalize_urls'], $config);
82 }
83
84 if (!empty($this->tasks['fake_response'])) {
85 $this->validateFakeResponse($p, $this->tasks, $rounds, $config);
86 }
87
88 if ($validateRealResponse) {
89 $this->validateRealResponse($p, $this->tasks, $rounds, $config);
90 }
91 }
92
93 /**
94 * Checks if the main library detects this provider
95 *
96 * @param string $providerName Provider Name
97 * @param array $tasks
98 * @param array $config
99 * @return void
100 *
101 * @medium
102 */
103 protected function validateCollectionDetection($providerName, array $tasks, array $config = [])
104 {
105 $config = array_merge([
106 'https_only' => false,
107 'fake_responses' => Embera::ALLOW_FAKE_RESPONSES,
108 'maxwidth' => 430,
109 'maxheight' => 270,
110 ], $config);
111
112 $collection = new DefaultProviderCollection($config);
113 $providers = $collection->findProviders($tasks['valid_urls']);
114
115 $this->assertNotEmpty($providers, 'Could not detect urls from the main Embera library.');
116 foreach ($providers as $p) {
117 $this->assertEquals($p->getProviderName(), $providerName);
118 }
119 }
120
121 /**
122 * Checks if all valid urls are correctly detected
123 *
124 * @param string $providerName Provider Name
125 * @param array $tasks
126 * @param array $config
127 * @return void
128 *
129 * @medium
130 */
131 protected function validateUrlDetection($providerName, array $tasks, array $config = [])
132 {
133 $validCount = 0;
134 $validUrlCount = count($tasks['valid_urls']);
135
136 foreach ($tasks['valid_urls'] as $valid) {
137 $reflection = new ReflectionClass('Embera\Provider\\' . $providerName);
138 $provider = $reflection->newInstance($valid, $config);
139 if ($provider->validateUrl($provider->getUrl(false))) {
140 $validCount++;
141 } else {
142 $this->fail(sprintf('The url %s was not recognized as valid', $valid));
143 }
144
145 }
146
147 $this->assertEquals($validUrlCount, $validCount, sprintf('The provider %s has problem detecting valid urls.', $providerName));
148
149 $validCount = 0;
150 foreach (array_merge($tasks['valid_urls'], $tasks['invalid_urls']) as $mixed) {
151 $reflection = new ReflectionClass('Embera\Provider\\' . $providerName);
152 $provider = $reflection->newInstance($mixed, $config);
153 if ($provider->validateUrl($provider->getUrl(false))) {
154 $validCount++;
155 }
156 }
157
158 $this->assertEquals($validUrlCount, $validCount, sprintf('The provider %s is detecting wrong urls as valid.', $providerName));
159 }
160
161 /**
162 * Validates that a url on this provider is correctly normalized
163 *
164 * @param string $providerName
165 * @param array $data
166 * @param array $config
167 * @return void
168 *
169 * @medium
170 */
171 protected function validateUrlNormalization($providerName, array $data, array $config = [])
172 {
173 $reflection = new ReflectionClass('Embera\Provider\\' . $providerName);
174 foreach ($data as $givenUrl => $expectedUrl) {
175 $provider = $reflection->newInstance($givenUrl, $config);
176 $normalizedUrl = $provider->getUrl();
177
178 $this->assertEquals($expectedUrl, $normalizedUrl, sprintf('We expected %s and were given %s', $expectedUrl, $normalizedUrl));
179 }
180 }
181
182 /**
183 * Validates a fake response
184 *
185 * @param string $providerName
186 * @param array $tasks
187 * @param int $rounds
188 * @param array $config
189 *
190 * @return void
191 *
192 * @large
193 */
194 protected function validateFakeResponse($providerName, array $tasks, $rounds, array $config = [])
195 {
196 // Percentage accepted for fake response similarity
197 $similarMinimumThreshold = 80;
198
199 $urls = $this->getRandomDataFrom($tasks['valid_urls'], $rounds);
200
201 $fakeOembedClient = new OembedClient(array_merge($config, [
202 'fake_responses' => Embera::ONLY_FAKE_RESPONSES,
203 ]), new HttpClient());
204
205 $realOembedClient = new OembedClient(array_merge($config, [
206 'fake_responses' => Embera::DISABLE_FAKE_RESPONSES,
207 ]), new HttpClient());
208
209 foreach ($urls as $url) {
210 $reflection = new ReflectionClass('Embera\Provider\\' . $providerName);
211 $provider = $reflection->newInstance($url, $config);
212
213 $fakeResponse = $fakeOembedClient->getResponseFrom($provider);
214
215 $this->assertNotEmpty($fakeResponse['html'], sprintf(
216 'Fake Response doesnt have html data on url %s', $url
217 ));
218
219 $this->assertStringContainsStringIgnoringCase($tasks['fake_response']['html'], $fakeResponse['html'], sprintf(
220 'Could not determine the existance of %s in the url %s. The response was %s',
221 $tasks['fake_response']['html'],
222 $url,
223 $fakeResponse['html']
224 ));
225
226 $types = $tasks['fake_response']['type'];
227 if (strpos($types, '|') !== false) {
228 $typesArray = explode('|', $types);
229 $this->assertContains($fakeResponse['type'], $typesArray, sprintf(
230 'Fake Response is type %s but we only are allowing %s',
231 $fakeResponse['type'], $types
232 ));
233 } else {
234 $this->assertEquals($types, $fakeResponse['type'], sprintf(
235 'Fake Response type is not type %s on %s, %s given',
236 $types,
237 $url,
238 $fakeResponse['type']
239 ));
240 }
241
242 $this->assertEquals(1, $fakeResponse['embera_using_fake_response'], sprintf(
243 'Fake response flag is not correct. Expecting "1", recieved "%s" on url %s',
244 $fakeResponse['embera_using_fake_response'], $url
245 ));
246
247 $realResponse = $realOembedClient->getResponseFrom($provider);
248 if (!isset($realResponse['embera_using_fake_response'])) {
249 $this->markTestIncomplete(sprintf(
250 'The Provider %s doesnt define the embera_using_fake_response on url %s. Probably the response took to long',
251 $providerName, $url
252 ));
253
254 return ;
255 }
256
257 $this->assertEquals(0, $realResponse['embera_using_fake_response'], sprintf(
258 'Fake response flag is not correct. Expecting "0", recieved "%s" on url %s',
259 $realResponse['embera_using_fake_response'], $url
260 ));
261
262 $this->assertNotEmpty($realResponse['html'], sprintf(
263 'Real Response doesnt have html data on url %s', $url
264 ));
265
266 if (!empty($realResponse['type']) && strpos($types, '|') === false) {
267 $this->assertEquals(strtolower($realResponse['type']), strtolower($fakeResponse['type']), sprintf(
268 'The real response is type %s and not %s',
269 $realResponse['type'],
270 $fakeResponse['type']
271 ));
272 }
273
274 similar_text($fakeResponse['html'], $realResponse['html'], $percent);
275 $this->assertTrue($percent >= $similarMinimumThreshold, sprintf(
276 'The Fake/Real response for url %s is off. The current threshold is %s and we got %s. Real response: "%s" | Fake response: "%s"',
277 $url, $similarMinimumThreshold, $percent, $realResponse['html'], $fakeResponse['html']
278 ));
279 }
280
281 }
282
283 /**
284 * Validates the real response from a given provider
285 *
286 * @param string $providerName
287 * @param array $tasks
288 * @param int $rounds
289 * @param array $config
290 *
291 * @return void
292 *
293 * @large
294 */
295 protected function validateRealResponse($providerName, array $tasks, $rounds, array $config = [])
296 {
297 $urls = $this->getRandomDataFrom($tasks['valid_urls'], $rounds);
298 $oembedClient = new OembedClient(array_merge($config, [
299 'fake_responses' => Embera::DISABLE_FAKE_RESPONSES,
300 ]), new HttpClient());
301
302 foreach ($urls as $url) {
303 $reflection = new ReflectionClass('Embera\Provider\\' . $providerName);
304 $provider = $reflection->newInstance($url, $config);
305
306 $response = $oembedClient->getResponseFrom($provider);
307 if (!isset($response['embera_using_fake_response'])) {
308 $this->markTestIncomplete(sprintf(
309 'The Provider %s doesnt define the embera_using_fake_response on url %s. Probably the response took to long',
310 $providerName, $url
311 ));
312
313 return ;
314 }
315
316 $this->assertEquals(0, $response['embera_using_fake_response'], sprintf(
317 'Fake response flag is not correct. Expecting "0", recieved "%s" on url %s',
318 $response['embera_using_fake_response'], $url
319 ));
320 }
321 }
322
323 }
324