Merge "Fix documentation in Linker::formatTemplates."
[lhc/web/wiklou.git] / tests / phpunit / includes / OutputPageTest.php
1 <?php
2
3 /**
4 *
5 * @author Matthew Flaschen
6 *
7 * @group Output
8 *
9 */
10 class OutputPageTest extends MediaWikiTestCase {
11 const SCREEN_MEDIA_QUERY = 'screen and (min-width: 982px)';
12 const SCREEN_ONLY_MEDIA_QUERY = 'only screen and (min-width: 982px)';
13
14 /**
15 * Tests a particular case of transformCssMedia, using the given input, globals,
16 * expected return, and message
17 *
18 * Asserts that $expectedReturn is returned.
19 *
20 * options['printableQuery'] - value of query string for printable, or omitted for none
21 * options['handheldQuery'] - value of query string for handheld, or omitted for none
22 * options['handheldForIPhone'] - value of the $wgHandheldForIPhone global
23 * options['media'] - passed into the method under the same name
24 * options['expectedReturn'] - expected return value
25 * options['message'] - PHPUnit message for assertion
26 *
27 * @param array $args key-value array of arguments as shown above
28 */
29 protected function assertTransformCssMediaCase( $args ) {
30 $queryData = array();
31 if ( isset( $args['printableQuery'] ) ) {
32 $queryData['printable'] = $args['printableQuery'];
33 }
34
35 if ( isset( $args['handheldQuery'] ) ) {
36 $queryData['handheld'] = $args['handheldQuery'];
37 }
38
39 $fauxRequest = new FauxRequest( $queryData, false );
40 $this->setMWGlobals( array(
41 'wgRequest' => $fauxRequest,
42 'wgHandheldForIPhone' => $args['handheldForIPhone']
43 ) );
44
45 $actualReturn = OutputPage::transformCssMedia( $args['media'] );
46 $this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] );
47 }
48
49 /**
50 * Tests a case of transformCssMedia with both values of wgHandheldForIPhone.
51 * Used to verify that behavior is orthogonal to that option.
52 *
53 * If the value of wgHandheldForIPhone should matter, use assertTransformCssMediaCase.
54 *
55 * @param array $args key-value array of arguments as shown in assertTransformCssMediaCase.
56 * Will be mutated.
57 */
58 protected function assertTransformCssMediaCaseWithBothHandheldForIPhone( $args ) {
59 $message = $args['message'];
60 foreach ( array( true, false ) as $handheldForIPhone ) {
61 $args['handheldForIPhone'] = $handheldForIPhone;
62 $stringHandheldForIPhone = var_export( $handheldForIPhone, true );
63 $args['message'] = "$message. \$wgHandheldForIPhone was $stringHandheldForIPhone";
64 $this->assertTransformCssMediaCase( $args );
65 }
66 }
67
68 /**
69 * Tests print requests
70 */
71 public function testPrintRequests() {
72 $this->assertTransformCssMediaCaseWithBothHandheldForIPhone( array(
73 'printableQuery' => '1',
74 'media' => 'screen',
75 'expectedReturn' => null,
76 'message' => 'On printable request, screen returns null'
77 ) );
78
79 $this->assertTransformCssMediaCaseWithBothHandheldForIPhone( array(
80 'printableQuery' => '1',
81 'media' => self::SCREEN_MEDIA_QUERY,
82 'expectedReturn' => null,
83 'message' => 'On printable request, screen media query returns null'
84 ) );
85
86 $this->assertTransformCssMediaCaseWithBothHandheldForIPhone( array(
87 'printableQuery' => '1',
88 'media' => self::SCREEN_ONLY_MEDIA_QUERY,
89 'expectedReturn' => null,
90 'message' => 'On printable request, screen media query with only returns null'
91 ) );
92
93 $this->assertTransformCssMediaCaseWithBothHandheldForIPhone( array(
94 'printableQuery' => '1',
95 'media' => 'print',
96 'expectedReturn' => '',
97 'message' => 'On printable request, media print returns empty string'
98 ) );
99 }
100
101 /**
102 * Tests screen requests, without either query parameter set
103 */
104 public function testScreenRequests() {
105 $this->assertTransformCssMediaCase( array(
106 'handheldForIPhone' => false,
107 'media' => 'screen',
108 'expectedReturn' => 'screen',
109 'message' => 'On screen request, with handheldForIPhone false, screen media type is preserved'
110 ) );
111
112 $this->assertTransformCssMediaCaseWithBothHandheldForIPhone( array(
113 'media' => self::SCREEN_MEDIA_QUERY,
114 'expectedReturn' => self::SCREEN_MEDIA_QUERY,
115 'message' => 'On screen request, screen media query is preserved.'
116 ) );
117
118 $this->assertTransformCssMediaCaseWithBothHandheldForIPhone( array(
119 'media' => self::SCREEN_ONLY_MEDIA_QUERY,
120 'expectedReturn' => self::SCREEN_ONLY_MEDIA_QUERY,
121 'message' => 'On screen request, screen media query with only is preserved.'
122 ) );
123
124 $this->assertTransformCssMediaCaseWithBothHandheldForIPhone( array(
125 'media' => 'print',
126 'expectedReturn' => 'print',
127 'message' => 'On screen request, print media type is preserved'
128 ) );
129 }
130
131 /**
132 * Tests handheld and wgHandheldForIPhone behavior
133 */
134 public function testHandheld() {
135 $this->assertTransformCssMediaCaseWithBothHandheldForIPhone( array(
136 'handheldQuery' => '1',
137 'media' => 'handheld',
138 'expectedReturn' => '',
139 'message' => 'On request with handheld querystring and media is handheld, returns empty string'
140 ) );
141
142 $this->assertTransformCssMediaCaseWithBothHandheldForIPhone( array(
143 'handheldQuery' => '1',
144 'media' => 'screen',
145 'expectedReturn' => null,
146 'message' => 'On request with handheld querystring and media is screen, returns null'
147 ) );
148
149 // A bit counter-intuitively, $wgHandheldForIPhone should only matter if the query handheld is false or omitted
150 $this->assertTransformCssMediaCase( array(
151 'handheldQuery' => '0',
152 'media' => 'screen',
153 'handheldForIPhone' => true,
154 'expectedReturn' => 'screen and (min-device-width: 481px)',
155 'message' => 'With $wgHandheldForIPhone true, screen media type is transformed'
156 ) );
157
158 $this->assertTransformCssMediaCase( array(
159 'media' => 'handheld',
160 'handheldForIPhone' => true,
161 'expectedReturn' => 'handheld, only screen and (max-device-width: 480px)',
162 'message' => 'With $wgHandheldForIPhone true, handheld media type is transformed'
163 ) );
164
165 $this->assertTransformCssMediaCase( array(
166 'media' => 'handheld',
167 'handheldForIPhone' => false,
168 'expectedReturn' => 'handheld',
169 'message' => 'With $wgHandheldForIPhone false, handheld media type is preserved'
170 ) );
171 }
172 }