8634aff6f9d1c290df6adcd748361cfe7673db27
[yaffs-website] / vendor / symfony / http-kernel / Tests / HttpCache / SsiTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests\HttpCache;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\HttpKernel\HttpCache\Ssi;
18
19 class SsiTest extends TestCase
20 {
21     public function testHasSurrogateSsiCapability()
22     {
23         $ssi = new Ssi();
24
25         $request = Request::create('/');
26         $request->headers->set('Surrogate-Capability', 'abc="SSI/1.0"');
27         $this->assertTrue($ssi->hasSurrogateCapability($request));
28
29         $request = Request::create('/');
30         $request->headers->set('Surrogate-Capability', 'foobar');
31         $this->assertFalse($ssi->hasSurrogateCapability($request));
32
33         $request = Request::create('/');
34         $this->assertFalse($ssi->hasSurrogateCapability($request));
35     }
36
37     public function testAddSurrogateSsiCapability()
38     {
39         $ssi = new Ssi();
40
41         $request = Request::create('/');
42         $ssi->addSurrogateCapability($request);
43         $this->assertEquals('symfony2="SSI/1.0"', $request->headers->get('Surrogate-Capability'));
44
45         $ssi->addSurrogateCapability($request);
46         $this->assertEquals('symfony2="SSI/1.0", symfony2="SSI/1.0"', $request->headers->get('Surrogate-Capability'));
47     }
48
49     public function testAddSurrogateControl()
50     {
51         $ssi = new Ssi();
52
53         $response = new Response('foo <!--#include virtual="" -->');
54         $ssi->addSurrogateControl($response);
55         $this->assertEquals('content="SSI/1.0"', $response->headers->get('Surrogate-Control'));
56
57         $response = new Response('foo');
58         $ssi->addSurrogateControl($response);
59         $this->assertEquals('', $response->headers->get('Surrogate-Control'));
60     }
61
62     public function testNeedsSsiParsing()
63     {
64         $ssi = new Ssi();
65
66         $response = new Response();
67         $response->headers->set('Surrogate-Control', 'content="SSI/1.0"');
68         $this->assertTrue($ssi->needsParsing($response));
69
70         $response = new Response();
71         $this->assertFalse($ssi->needsParsing($response));
72     }
73
74     public function testRenderIncludeTag()
75     {
76         $ssi = new Ssi();
77
78         $this->assertEquals('<!--#include virtual="/" -->', $ssi->renderIncludeTag('/', '/alt', true));
79         $this->assertEquals('<!--#include virtual="/" -->', $ssi->renderIncludeTag('/', '/alt', false));
80         $this->assertEquals('<!--#include virtual="/" -->', $ssi->renderIncludeTag('/'));
81     }
82
83     public function testProcessDoesNothingIfContentTypeIsNotHtml()
84     {
85         $ssi = new Ssi();
86
87         $request = Request::create('/');
88         $response = new Response();
89         $response->headers->set('Content-Type', 'text/plain');
90         $ssi->process($request, $response);
91
92         $this->assertFalse($response->headers->has('x-body-eval'));
93     }
94
95     public function testProcess()
96     {
97         $ssi = new Ssi();
98
99         $request = Request::create('/');
100         $response = new Response('foo <!--#include virtual="..." -->');
101         $ssi->process($request, $response);
102
103         $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent());
104         $this->assertEquals('SSI', $response->headers->get('x-body-eval'));
105
106         $response = new Response('foo <!--#include virtual="foo\'" -->');
107         $ssi->process($request, $response);
108
109         $this->assertEquals("foo <?php echo \$this->surrogate->handle(\$this, 'foo\\'', '', false) ?>"."\n", $response->getContent());
110     }
111
112     public function testProcessEscapesPhpTags()
113     {
114         $ssi = new Ssi();
115
116         $request = Request::create('/');
117         $response = new Response('<?php <? <% <script language=php>');
118         $ssi->process($request, $response);
119
120         $this->assertEquals('<?php echo "<?"; ?>php <?php echo "<?"; ?> <?php echo "<%"; ?> <?php echo "<s"; ?>cript language=php>', $response->getContent());
121     }
122
123     /**
124      * @expectedException \RuntimeException
125      */
126     public function testProcessWhenNoSrcInAnSsi()
127     {
128         $ssi = new Ssi();
129
130         $request = Request::create('/');
131         $response = new Response('foo <!--#include -->');
132         $ssi->process($request, $response);
133     }
134
135     public function testProcessRemoveSurrogateControlHeader()
136     {
137         $ssi = new Ssi();
138
139         $request = Request::create('/');
140         $response = new Response('foo <!--#include virtual="..." -->');
141         $response->headers->set('Surrogate-Control', 'content="SSI/1.0"');
142         $ssi->process($request, $response);
143         $this->assertEquals('SSI', $response->headers->get('x-body-eval'));
144
145         $response->headers->set('Surrogate-Control', 'no-store, content="SSI/1.0"');
146         $ssi->process($request, $response);
147         $this->assertEquals('SSI', $response->headers->get('x-body-eval'));
148         $this->assertEquals('no-store', $response->headers->get('surrogate-control'));
149
150         $response->headers->set('Surrogate-Control', 'content="SSI/1.0", no-store');
151         $ssi->process($request, $response);
152         $this->assertEquals('SSI', $response->headers->get('x-body-eval'));
153         $this->assertEquals('no-store', $response->headers->get('surrogate-control'));
154     }
155
156     public function testHandle()
157     {
158         $ssi = new Ssi();
159         $cache = $this->getCache(Request::create('/'), new Response('foo'));
160         $this->assertEquals('foo', $ssi->handle($cache, '/', '/alt', true));
161     }
162
163     /**
164      * @expectedException \RuntimeException
165      */
166     public function testHandleWhenResponseIsNot200()
167     {
168         $ssi = new Ssi();
169         $response = new Response('foo');
170         $response->setStatusCode(404);
171         $cache = $this->getCache(Request::create('/'), $response);
172         $ssi->handle($cache, '/', '/alt', false);
173     }
174
175     public function testHandleWhenResponseIsNot200AndErrorsAreIgnored()
176     {
177         $ssi = new Ssi();
178         $response = new Response('foo');
179         $response->setStatusCode(404);
180         $cache = $this->getCache(Request::create('/'), $response);
181         $this->assertEquals('', $ssi->handle($cache, '/', '/alt', true));
182     }
183
184     public function testHandleWhenResponseIsNot200AndAltIsPresent()
185     {
186         $ssi = new Ssi();
187         $response1 = new Response('foo');
188         $response1->setStatusCode(404);
189         $response2 = new Response('bar');
190         $cache = $this->getCache(Request::create('/'), array($response1, $response2));
191         $this->assertEquals('bar', $ssi->handle($cache, '/', '/alt', false));
192     }
193
194     protected function getCache($request, $response)
195     {
196         $cache = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpCache\HttpCache')->setMethods(array('getRequest', 'handle'))->disableOriginalConstructor()->getMock();
197         $cache->expects($this->any())
198               ->method('getRequest')
199               ->will($this->returnValue($request))
200         ;
201         if (is_array($response)) {
202             $cache->expects($this->any())
203                   ->method('handle')
204                   ->will(call_user_func_array(array($this, 'onConsecutiveCalls'), $response))
205             ;
206         } else {
207             $cache->expects($this->any())
208                   ->method('handle')
209                   ->will($this->returnValue($response))
210             ;
211         }
212
213         return $cache;
214     }
215 }