|
1 | 1 | <?php |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
2 | 5 |
|
3 | | -require '../../Bugzilla.class.php'; |
4 | | -require '../../BugzillaQuery.class.php'; |
5 | | - |
6 | | -class BugzillaQueryTest extends PHPUnit_Framework_TestCase |
| 6 | +/** |
| 7 | + * @covers \BugzillaQuery |
| 8 | + * @covers \BugzillaBaseQuery |
| 9 | + * @covers \BugzillaRESTQuery |
| 10 | + */ |
| 11 | +class BugzillaQueryTest extends MediaWikiIntegrationTestCase |
7 | 12 | { |
| 13 | + |
| 14 | + protected function setUp(): void |
| 15 | + { |
| 16 | + parent::setUp(); |
| 17 | + |
| 18 | + $this->setMwGlobals([ |
| 19 | + 'wgBugzillaMethod' => 'REST', |
| 20 | + 'wgBugzillaRESTURL' => 'https://bugzilla.example.org/rest', |
| 21 | + 'wgBugzillaURL' => 'https://bugzilla.example.org', |
| 22 | + 'wgBugzillaDefaultFields' => ['id', 'summary', 'priority', 'status'], |
| 23 | + ]); |
| 24 | + } |
| 25 | + |
8 | 26 | /** |
9 | 27 | * @dataProvider prepareOptionsProvider |
10 | 28 | * |
11 | 29 | * @param string $json JSON options structure defined in <bugzilla /> element. |
12 | 30 | * @param array $default default fields to include in query |
13 | 31 | * @param array $expected resulting options array |
14 | | - */ |
| 32 | + */ |
15 | 33 | public function testPrepareOptions($json, $default, $expected) |
16 | 34 | { |
17 | 35 | $q = BugzillaQuery::create('bug', $json, 'title'); |
18 | 36 | $this->assertEquals($expected, $q->prepare_options($json, $default)); |
19 | 37 | } |
20 | 38 |
|
21 | | - public function prepareOptionsProvider() |
| 39 | + public static function prepareOptionsProvider() |
22 | 40 | { |
23 | 41 | $default_includes = ['incA', 'incB']; |
24 | 42 | return [ |
25 | | - [ |
| 43 | + 'no query at all falls back to the default fields' => [ |
26 | 44 | '', |
27 | 45 | [], |
28 | 46 | ['include_fields' => []] |
29 | 47 | ], |
30 | | - [ |
| 48 | + 'an empty JSON object falls back to the default fields' => [ |
31 | 49 | " { \n } ", |
32 | 50 | ['default'], |
33 | 51 | ['include_fields' => ['default']] |
34 | 52 | ], |
35 | | - [ |
| 53 | + 'other options are kept alongside the default fields' => [ |
36 | 54 | '{"other":"options"}', |
37 | 55 | $default_includes, |
38 | 56 | [ |
39 | 57 | 'include_fields' => $default_includes, |
40 | 58 | 'other' => 'options' |
41 | 59 | ] |
42 | 60 | ], |
43 | | - [ |
| 61 | + 'a single requested field replaces the defaults' => [ |
44 | 62 | '{"include_fields": ["C"]}', |
45 | 63 | $default_includes, |
46 | 64 | [ |
47 | 65 | 'include_fields' => ['C'] |
48 | 66 | ] |
49 | 67 | ], |
50 | | - [ |
| 68 | + 'a JSON array of fields is used as given' => [ |
51 | 69 | '{"include_fields": ["json", "array"]}', |
52 | 70 | $default_includes, |
53 | 71 | [ |
54 | 72 | 'include_fields' => ['json', 'array'] |
55 | 73 | ] |
56 | 74 | ], |
57 | | - [ |
| 75 | + 'a comma separated string of fields is split' => [ |
58 | 76 | '{"include_fields": "json,string"}', |
59 | 77 | $default_includes, |
60 | 78 | [ |
61 | 79 | 'include_fields' => ['json', 'string'] |
62 | 80 | ] |
63 | 81 | ], |
64 | | - [ |
| 82 | + 'invalid JSON produces no options' => [ |
65 | 83 | 'invalid JSON', |
66 | 84 | $default_includes, |
67 | 85 | null |
68 | 86 | ], |
69 | 87 | ]; |
70 | 88 | } |
71 | 89 |
|
72 | | - public function testPrepareOptionsError() |
| 90 | + public function testInvalidJsonOptionsAreReportedAsAnError() |
73 | 91 | { |
74 | 92 | $q = BugzillaQuery::create('bug', 'invalid JSON', 'title'); |
75 | | - $this->assertEquals(null, $q->prepare_options('invalid JSON', ['A', 'B'])); |
76 | | - $this->assertEquals('Query options must be valid JSON.', $q->error); |
| 93 | + |
| 94 | + $this->assertSame('Query options must be valid JSON.', $q->error); |
77 | 95 | } |
78 | 96 |
|
79 | 97 | /** |
80 | 98 | * @dataProvider rebaseFieldsProvider |
81 | | - */ |
| 99 | + */ |
82 | 100 | public function testRebaseFields($request, $synthetic, $expected) |
83 | 101 | { |
84 | 102 | $q = BugzillaQuery::create('bug', '{}', 'title'); |
| 103 | + |
85 | 104 | $this->assertEquals($expected, $q->rebase_fields($request, $synthetic)); |
86 | 105 | } |
87 | 106 |
|
88 | | - public function rebaseFieldsProvider() |
| 107 | + public static function rebaseFieldsProvider() |
89 | 108 | { |
90 | 109 | return [ |
91 | | - [ ['A', 'B', 'C'], ['A', 'B'], ['A', 'B', 'C'] ], |
92 | | - [ ['A'], ['A', 'B'], ['A', 'B'] ], |
93 | | - [ ['C'], ['B', 'A'], ['A', 'B', 'C'] ], |
| 110 | + 'already a superset of the synthetic fields' => [ ['A', 'B', 'C'], ['A', 'B'], ['A', 'B', 'C'] ], |
| 111 | + 'missing synthetic fields are added' => [ ['A'], ['A', 'B'], ['A', 'B'] ], |
| 112 | + 'the result is sorted' => [ ['C'], ['B', 'A'], ['A', 'B', 'C'] ], |
94 | 113 | ]; |
95 | 114 | } |
96 | 115 |
|
| 116 | + public function testFieldsNeededOnlyForRenderingAreFetchedButNotDisplayed() |
| 117 | + { |
| 118 | + $q = BugzillaQuery::create('bug', '{"include_fields": ["summary"]}', 'title'); |
| 119 | + |
| 120 | + $this->assertSame( |
| 121 | + ['summary'], |
| 122 | + $q->options['include_fields'], |
| 123 | + 'the columns are the ones the wiki page asked for' |
| 124 | + ); |
| 125 | + $this->assertSame( |
| 126 | + ['id', 'priority', 'status', 'summary'], |
| 127 | + $q->rebased_options()['include_fields'], |
| 128 | + 'the request also carries the fields the templates need' |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + public function testTheFullQueryUrlRepeatsEachValueOfAnArrayOption() |
| 133 | + { |
| 134 | + $q = BugzillaQuery::create( |
| 135 | + 'bug', |
| 136 | + '{"product": "Bugzilla", "include_fields": ["id", "summary"]}', |
| 137 | + 'title' |
| 138 | + ); |
| 139 | + |
| 140 | + $this->assertSame( |
| 141 | + 'https://bugzilla.example.org/buglist.cgi' |
| 142 | + . '?product=Bugzilla&include_fields=id&include_fields=summary', |
| 143 | + $q->full_query_url() |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + public function testTheFullQueryUrlEscapesValues() |
| 148 | + { |
| 149 | + $q = BugzillaQuery::create('bug', '{"whiteboard": "[needs triage]"}', 'title'); |
| 150 | + |
| 151 | + $this->assertStringContainsString( |
| 152 | + 'whiteboard=%5Bneeds+triage%5D', |
| 153 | + $q->full_query_url() |
| 154 | + ); |
| 155 | + } |
97 | 156 | } |
0 commit comments