Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\update\Unit;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Tests\UnitTestCase;
|
Chris@0
|
6 use Drupal\update\UpdateFetcher;
|
Chris@0
|
7
|
Chris@0
|
8 if (!defined('DRUPAL_CORE_COMPATIBILITY')) {
|
Chris@0
|
9 define('DRUPAL_CORE_COMPATIBILITY', '8.x');
|
Chris@0
|
10 }
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Tests update functionality unrelated to the database.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @group update
|
Chris@0
|
16 */
|
Chris@0
|
17 class UpdateFetcherTest extends UnitTestCase {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * The update fetcher to use.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var \Drupal\update\UpdateFetcher
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $updateFetcher;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * {@inheritdoc}
|
Chris@0
|
28 */
|
Chris@0
|
29 protected function setUp() {
|
Chris@0
|
30 $config_factory = $this->getConfigFactoryStub(['update.settings' => ['fetch_url' => 'http://www.example.com']]);
|
Chris@12
|
31 $http_client_mock = $this->createMock('\GuzzleHttp\ClientInterface');
|
Chris@0
|
32 $this->updateFetcher = new UpdateFetcher($config_factory, $http_client_mock);
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * Tests that buildFetchUrl() builds the URL correctly.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @param array $project
|
Chris@0
|
39 * A keyed array of project information matching results from
|
Chris@0
|
40 * \Drupal\Update\UpdateManager::getProjects().
|
Chris@0
|
41 * @param string $site_key
|
Chris@0
|
42 * A string to mimic an anonymous site key hash.
|
Chris@0
|
43 * @param string $expected
|
Chris@0
|
44 * The expected url returned from UpdateFetcher::buildFetchUrl()
|
Chris@0
|
45 *
|
Chris@0
|
46 * @dataProvider providerTestUpdateBuildFetchUrl
|
Chris@0
|
47 *
|
Chris@0
|
48 * @see \Drupal\update\UpdateFetcher::buildFetchUrl()
|
Chris@0
|
49 */
|
Chris@0
|
50 public function testUpdateBuildFetchUrl(array $project, $site_key, $expected) {
|
Chris@0
|
51 $url = $this->updateFetcher->buildFetchUrl($project, $site_key);
|
Chris@0
|
52 $this->assertEquals($url, $expected);
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * Provide test data for self::testUpdateBuildFetchUrl().
|
Chris@0
|
57 *
|
Chris@0
|
58 * @return array
|
Chris@0
|
59 * An array of arrays, each containing:
|
Chris@0
|
60 * - 'project' - An array matching a project's .info file structure.
|
Chris@0
|
61 * - 'site_key' - An arbitrary site key.
|
Chris@0
|
62 * - 'expected' - The expected url from UpdateFetcher::buildFetchUrl().
|
Chris@0
|
63 */
|
Chris@0
|
64 public function providerTestUpdateBuildFetchUrl() {
|
Chris@0
|
65 $data = [];
|
Chris@0
|
66
|
Chris@0
|
67 // First test that we didn't break the trivial case.
|
Chris@0
|
68 $project['name'] = 'update_test';
|
Chris@0
|
69 $project['project_type'] = '';
|
Chris@0
|
70 $project['info']['version'] = '';
|
Chris@0
|
71 $project['info']['project status url'] = 'http://www.example.com';
|
Chris@0
|
72 $project['includes'] = ['module1' => 'Module 1', 'module2' => 'Module 2'];
|
Chris@0
|
73 $site_key = '';
|
Chris@0
|
74 $expected = 'http://www.example.com/' . $project['name'] . '/' . DRUPAL_CORE_COMPATIBILITY;
|
Chris@0
|
75
|
Chris@0
|
76 $data[] = [$project, $site_key, $expected];
|
Chris@0
|
77
|
Chris@0
|
78 // For disabled projects it shouldn't add the site key either.
|
Chris@0
|
79 $site_key = 'site_key';
|
Chris@0
|
80 $project['project_type'] = 'disabled';
|
Chris@0
|
81 $expected = 'http://www.example.com/' . $project['name'] . '/' . DRUPAL_CORE_COMPATIBILITY;
|
Chris@0
|
82
|
Chris@0
|
83 $data[] = [$project, $site_key, $expected];
|
Chris@0
|
84
|
Chris@0
|
85 // For enabled projects, test adding the site key.
|
Chris@0
|
86 $project['project_type'] = '';
|
Chris@0
|
87 $expected = 'http://www.example.com/' . $project['name'] . '/' . DRUPAL_CORE_COMPATIBILITY;
|
Chris@0
|
88 $expected .= '?site_key=site_key';
|
Chris@0
|
89 $expected .= '&list=' . rawurlencode('module1,module2');
|
Chris@0
|
90
|
Chris@0
|
91 $data[] = [$project, $site_key, $expected];
|
Chris@0
|
92
|
Chris@0
|
93 // Test when the URL contains a question mark.
|
Chris@0
|
94 $project['info']['project status url'] = 'http://www.example.com/?project=';
|
Chris@0
|
95 $expected = 'http://www.example.com/?project=/' . $project['name'] . '/' . DRUPAL_CORE_COMPATIBILITY;
|
Chris@0
|
96 $expected .= '&site_key=site_key';
|
Chris@0
|
97 $expected .= '&list=' . rawurlencode('module1,module2');
|
Chris@0
|
98
|
Chris@0
|
99 $data[] = [$project, $site_key, $expected];
|
Chris@0
|
100
|
Chris@0
|
101 return $data;
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 }
|