3 namespace Drupal\Tests\update\Unit;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\update\UpdateFetcher;
8 if (!defined('DRUPAL_CORE_COMPATIBILITY')) {
9 define('DRUPAL_CORE_COMPATIBILITY', '8.x');
13 * Tests update functionality unrelated to the database.
17 class UpdateFetcherTest extends UnitTestCase {
20 * The update fetcher to use.
22 * @var \Drupal\update\UpdateFetcher
24 protected $updateFetcher;
29 protected function setUp() {
30 $config_factory = $this->getConfigFactoryStub(['update.settings' => ['fetch_url' => 'http://www.example.com']]);
31 $http_client_mock = $this->createMock('\GuzzleHttp\ClientInterface');
32 $this->updateFetcher = new UpdateFetcher($config_factory, $http_client_mock);
36 * Tests that buildFetchUrl() builds the URL correctly.
38 * @param array $project
39 * A keyed array of project information matching results from
40 * \Drupal\Update\UpdateManager::getProjects().
41 * @param string $site_key
42 * A string to mimic an anonymous site key hash.
43 * @param string $expected
44 * The expected url returned from UpdateFetcher::buildFetchUrl()
46 * @dataProvider providerTestUpdateBuildFetchUrl
48 * @see \Drupal\update\UpdateFetcher::buildFetchUrl()
50 public function testUpdateBuildFetchUrl(array $project, $site_key, $expected) {
51 $url = $this->updateFetcher->buildFetchUrl($project, $site_key);
52 $this->assertEquals($url, $expected);
56 * Provide test data for self::testUpdateBuildFetchUrl().
59 * An array of arrays, each containing:
60 * - 'project' - An array matching a project's .info file structure.
61 * - 'site_key' - An arbitrary site key.
62 * - 'expected' - The expected url from UpdateFetcher::buildFetchUrl().
64 public function providerTestUpdateBuildFetchUrl() {
67 // First test that we didn't break the trivial case.
68 $project['name'] = 'update_test';
69 $project['project_type'] = '';
70 $project['info']['version'] = '';
71 $project['info']['project status url'] = 'http://www.example.com';
72 $project['includes'] = ['module1' => 'Module 1', 'module2' => 'Module 2'];
74 $expected = 'http://www.example.com/' . $project['name'] . '/' . DRUPAL_CORE_COMPATIBILITY;
76 $data[] = [$project, $site_key, $expected];
78 // For disabled projects it shouldn't add the site key either.
79 $site_key = 'site_key';
80 $project['project_type'] = 'disabled';
81 $expected = 'http://www.example.com/' . $project['name'] . '/' . DRUPAL_CORE_COMPATIBILITY;
83 $data[] = [$project, $site_key, $expected];
85 // For enabled projects, test adding the site key.
86 $project['project_type'] = '';
87 $expected = 'http://www.example.com/' . $project['name'] . '/' . DRUPAL_CORE_COMPATIBILITY;
88 $expected .= '?site_key=site_key';
89 $expected .= '&list=' . rawurlencode('module1,module2');
91 $data[] = [$project, $site_key, $expected];
93 // Test when the URL contains a question mark.
94 $project['info']['project status url'] = 'http://www.example.com/?project=';
95 $expected = 'http://www.example.com/?project=/' . $project['name'] . '/' . DRUPAL_CORE_COMPATIBILITY;
96 $expected .= '&site_key=site_key';
97 $expected .= '&list=' . rawurlencode('module1,module2');
99 $data[] = [$project, $site_key, $expected];