61c767deda673e99aff5f4785083e523f7d39168
[yaffs-website] / web / core / modules / update / tests / src / Unit / UpdateFetcherTest.php
1 <?php
2
3 namespace Drupal\Tests\update\Unit;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\update\UpdateFetcher;
7
8 if (!defined('DRUPAL_CORE_COMPATIBILITY')) {
9   define('DRUPAL_CORE_COMPATIBILITY', '8.x');
10 }
11
12 /**
13  * Tests update functionality unrelated to the database.
14  *
15  * @group update
16  */
17 class UpdateFetcherTest extends UnitTestCase {
18
19   /**
20    * The update fetcher to use.
21    *
22    * @var \Drupal\update\UpdateFetcher
23    */
24   protected $updateFetcher;
25
26   /**
27    * {@inheritdoc}
28    */
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);
33   }
34
35   /**
36    * Tests that buildFetchUrl() builds the URL correctly.
37    *
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()
45    *
46    * @dataProvider providerTestUpdateBuildFetchUrl
47    *
48    * @see \Drupal\update\UpdateFetcher::buildFetchUrl()
49    */
50   public function testUpdateBuildFetchUrl(array $project, $site_key, $expected) {
51     $url = $this->updateFetcher->buildFetchUrl($project, $site_key);
52     $this->assertEquals($url, $expected);
53   }
54
55   /**
56    * Provide test data for self::testUpdateBuildFetchUrl().
57    *
58    * @return array
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().
63    */
64   public function providerTestUpdateBuildFetchUrl() {
65     $data = [];
66
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'];
73     $site_key = '';
74     $expected = 'http://www.example.com/' . $project['name'] . '/' . DRUPAL_CORE_COMPATIBILITY;
75
76     $data[] = [$project, $site_key, $expected];
77
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;
82
83     $data[] = [$project, $site_key, $expected];
84
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');
90
91     $data[] = [$project, $site_key, $expected];
92
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');
98
99     $data[] = [$project, $site_key, $expected];
100
101     return $data;
102   }
103
104 }