Completed
Push — master ( 7e9523...bccefb )
by
unknown
9s
created
src/Feed/CwpAtomFeed.php 1 patch
Indentation   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -18,57 +18,57 @@
 block discarded – undo
18 18
 
19 19
 class CwpAtomFeed extends RSSFeed
20 20
 {
21
-    public function __construct(
22
-        SS_List $entries,
23
-        $link,
24
-        $title,
25
-        $description = null,
26
-        $titleField = "Title",
27
-        $descriptionField = "Content",
28
-        $authorField = null,
29
-        $lastModified = null,
30
-        $etag = null
31
-    ) {
32
-        parent::__construct(
33
-            $entries,
34
-            $link,
35
-            $title,
36
-            $description,
37
-            $titleField,
38
-            $descriptionField,
39
-            $authorField,
40
-            $lastModified
41
-        );
21
+	public function __construct(
22
+		SS_List $entries,
23
+		$link,
24
+		$title,
25
+		$description = null,
26
+		$titleField = "Title",
27
+		$descriptionField = "Content",
28
+		$authorField = null,
29
+		$lastModified = null,
30
+		$etag = null
31
+	) {
32
+		parent::__construct(
33
+			$entries,
34
+			$link,
35
+			$title,
36
+			$description,
37
+			$titleField,
38
+			$descriptionField,
39
+			$authorField,
40
+			$lastModified
41
+		);
42 42
 
43
-        $this->setTemplate(__CLASS__);
44
-    }
43
+		$this->setTemplate(__CLASS__);
44
+	}
45 45
 
46
-    /**
47
-     * Include an link to the feed
48
-     *
49
-     * @param string $url URL of the feed
50
-     * @param string $title Title to show
51
-     */
52
-    public static function linkToFeed($url, $title = null)
53
-    {
54
-        $title = Convert::raw2xml($title);
55
-        Requirements::insertHeadTags(
56
-            '<link rel="alternate" type="application/atom+xml" title="' . $title .
57
-            '" href="' . $url . '" />'
58
-        );
59
-    }
46
+	/**
47
+	 * Include an link to the feed
48
+	 *
49
+	 * @param string $url URL of the feed
50
+	 * @param string $title Title to show
51
+	 */
52
+	public static function linkToFeed($url, $title = null)
53
+	{
54
+		$title = Convert::raw2xml($title);
55
+		Requirements::insertHeadTags(
56
+			'<link rel="alternate" type="application/atom+xml" title="' . $title .
57
+			'" href="' . $url . '" />'
58
+		);
59
+	}
60 60
 
61
-    /**
62
-     * Output the feed to the browser
63
-     *
64
-     * @return DBHTMLText
65
-     */
66
-    public function outputToBrowser()
67
-    {
68
-        $output = parent::outputToBrowser();
69
-        $response = Controller::curr()->getResponse();
70
-        $response->addHeader("Content-Type", "application/atom+xml");
61
+	/**
62
+	 * Output the feed to the browser
63
+	 *
64
+	 * @return DBHTMLText
65
+	 */
66
+	public function outputToBrowser()
67
+	{
68
+		$output = parent::outputToBrowser();
69
+		$response = Controller::curr()->getResponse();
70
+		$response->addHeader("Content-Type", "application/atom+xml");
71 71
 
72
-        return $output;
73
-    }
72
+		return $output;
73
+	}
74 74
 }
Please login to merge, or discard this patch.
src/Extension/RichLinksExtension.php 1 patch
Indentation   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -17,48 +17,48 @@
 block discarded – undo
17 17
 class RichLinksExtension extends Extension
18 18
 {
19 19
 
20
-    /**
21
-     * @var array
22
-     */
23
-    private static $casting = [
24
-        'RichLinks' => 'HTMLText'
25
-    ];
20
+	/**
21
+	 * @var array
22
+	 */
23
+	private static $casting = [
24
+		'RichLinks' => 'HTMLText'
25
+	];
26 26
 
27
-    /**
28
-     * @return string
29
-     */
30
-    public function RichLinks()
31
-    {
32
-        // Note:
33
-        // Assume we can use Regexes because the link will always be formatted
34
-        // in the same way coming from the CMS.
27
+	/**
28
+	 * @return string
29
+	 */
30
+	public function RichLinks()
31
+	{
32
+		// Note:
33
+		// Assume we can use Regexes because the link will always be formatted
34
+		// in the same way coming from the CMS.
35 35
 
36
-        $content = $this->owner->value;
36
+		$content = $this->owner->value;
37 37
 
38
-        // Find all file links for processing.
39
-        preg_match_all('/<a.*href="\[file_link,id=([0-9]+)\].*".*>.*<\/a>/U', $content, $matches);
38
+		// Find all file links for processing.
39
+		preg_match_all('/<a.*href="\[file_link,id=([0-9]+)\].*".*>.*<\/a>/U', $content, $matches);
40 40
 
41
-        // Attach the file type and size to each of the links.
42
-        for ($i = 0; $i < count($matches[0]); $i++) {
43
-            $file = DataObject::get_by_id(File::class, $matches[1][$i]);
44
-            if ($file) {
45
-                $size = $file->getSize();
46
-                $ext = strtoupper($file->getExtension());
47
-                // Replace the closing </a> tag with the size span (and reattach the closing tag).
48
-                $newLink = substr($matches[0][$i], 0, strlen($matches[0][$i]) - 4)
49
-                    . "<span class='fileExt'> [$ext, $size]</span></a>";
50
-                $content = str_replace($matches[0][$i], $newLink, $content);
51
-            }
52
-        }
41
+		// Attach the file type and size to each of the links.
42
+		for ($i = 0; $i < count($matches[0]); $i++) {
43
+			$file = DataObject::get_by_id(File::class, $matches[1][$i]);
44
+			if ($file) {
45
+				$size = $file->getSize();
46
+				$ext = strtoupper($file->getExtension());
47
+				// Replace the closing </a> tag with the size span (and reattach the closing tag).
48
+				$newLink = substr($matches[0][$i], 0, strlen($matches[0][$i]) - 4)
49
+					. "<span class='fileExt'> [$ext, $size]</span></a>";
50
+				$content = str_replace($matches[0][$i], $newLink, $content);
51
+			}
52
+		}
53 53
 
54
-        // Inject extra attributes into the external links.
55
-        $pattern = '/(<a.*)(href=\"https?:\/\/[^\"]*\"[^>]*>.*)(<\/a>)/iU';
56
-        $replacement = sprintf(
57
-            '$1class="external" rel="external" title="%s" $2<span class="nonvisual-indicator">(external link)</span>$3',
58
-            _t(__CLASS__ . '.OpenLinkTitle', 'Open external link')
59
-        );
60
-        $content = preg_replace($pattern, $replacement, $content, -1);
54
+		// Inject extra attributes into the external links.
55
+		$pattern = '/(<a.*)(href=\"https?:\/\/[^\"]*\"[^>]*>.*)(<\/a>)/iU';
56
+		$replacement = sprintf(
57
+			'$1class="external" rel="external" title="%s" $2<span class="nonvisual-indicator">(external link)</span>$3',
58
+			_t(__CLASS__ . '.OpenLinkTitle', 'Open external link')
59
+		);
60
+		$content = preg_replace($pattern, $replacement, $content, -1);
61 61
 
62
-        return $content;
63
-    }
62
+		return $content;
63
+	}
64 64
 }
Please login to merge, or discard this patch.
src/Control/InitialisationMiddleware.php 1 patch
Indentation   +92 added lines, -92 removed lines patch added patch discarded remove patch
@@ -12,97 +12,97 @@
 block discarded – undo
12 12
  */
13 13
 class InitialisationMiddleware implements HTTPMiddleware
14 14
 {
15
-    use Configurable;
16
-
17
-    /**
18
-     * Disable the automatically added 'X-XSS-Protection' header that is added to all responses. This should be left
19
-     * alone in most circumstances to include the header. Refer to Mozilla Developer Network for more information:
20
-     * https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection
21
-     *
22
-     * @config
23
-     * @var bool
24
-     */
25
-    private static $xss_protection_enabled = true;
26
-
27
-    /**
28
-     * Enable egress proxy. This works on the principle of setting http(s)_proxy environment variables,
29
-     *  which will be automatically picked up by curl. This means RestfulService and raw curl
30
-     *  requests should work out of the box. Stream-based requests need extra manual configuration.
31
-     *  Refer to https://www.cwp.govt.nz/guides/core-technical-documentation/common-web-platform-core/en/how-tos/external_http_requests_with_proxy
32
-     *
33
-     * @config
34
-     * @var bool
35
-     */
36
-    private static $egress_proxy_default_enabled = true;
37
-
38
-    /**
39
-     * Configure the list of domains to bypass proxy by setting the NO_PROXY environment variable.
40
-     * 'services.cwp.govt.nz' needs to be present for Solr and Docvert internal CWP integration.
41
-     * 'localhost' is necessary for accessing services on the same instance such as tika-server for text extraction.
42
-     *
43
-     * @config
44
-     * @var string[]
45
-     */
46
-    private static $egress_proxy_exclude_domains = [
47
-        'services.cwp.govt.nz',
48
-        'localhost',
49
-    ];
50
-
51
-    public function process(HTTPRequest $request, callable $delegate)
52
-    {
53
-        $response = $delegate($request);
54
-
55
-        if ($this->config()->get('egress_proxy_default_enabled')) {
56
-            $this->configureEgressProxy();
57
-        }
15
+	use Configurable;
16
+
17
+	/**
18
+	 * Disable the automatically added 'X-XSS-Protection' header that is added to all responses. This should be left
19
+	 * alone in most circumstances to include the header. Refer to Mozilla Developer Network for more information:
20
+	 * https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection
21
+	 *
22
+	 * @config
23
+	 * @var bool
24
+	 */
25
+	private static $xss_protection_enabled = true;
26
+
27
+	/**
28
+	 * Enable egress proxy. This works on the principle of setting http(s)_proxy environment variables,
29
+	 *  which will be automatically picked up by curl. This means RestfulService and raw curl
30
+	 *  requests should work out of the box. Stream-based requests need extra manual configuration.
31
+	 *  Refer to https://www.cwp.govt.nz/guides/core-technical-documentation/common-web-platform-core/en/how-tos/external_http_requests_with_proxy
32
+	 *
33
+	 * @config
34
+	 * @var bool
35
+	 */
36
+	private static $egress_proxy_default_enabled = true;
37
+
38
+	/**
39
+	 * Configure the list of domains to bypass proxy by setting the NO_PROXY environment variable.
40
+	 * 'services.cwp.govt.nz' needs to be present for Solr and Docvert internal CWP integration.
41
+	 * 'localhost' is necessary for accessing services on the same instance such as tika-server for text extraction.
42
+	 *
43
+	 * @config
44
+	 * @var string[]
45
+	 */
46
+	private static $egress_proxy_exclude_domains = [
47
+		'services.cwp.govt.nz',
48
+		'localhost',
49
+	];
50
+
51
+	public function process(HTTPRequest $request, callable $delegate)
52
+	{
53
+		$response = $delegate($request);
54
+
55
+		if ($this->config()->get('egress_proxy_default_enabled')) {
56
+			$this->configureEgressProxy();
57
+		}
58 58
         
59
-        $this->configureProxyDomainExclusions();
60
-
61
-        if ($this->config()->get('xss_protection_enabled') && $response) {
62
-            $response->addHeader('X-XSS-Protection', '1; mode=block');
63
-        }
64
-
65
-        return $response;
66
-    }
67
-
68
-    /**
69
-     * If the outbound egress proxy details have been defined in environment variables, configure the proxy
70
-     * variables that are used to configure it.
71
-     */
72
-    protected function configureEgressProxy()
73
-    {
74
-        if (!Environment::getEnv('SS_OUTBOUND_PROXY')
75
-            || !Environment::getEnv('SS_OUTBOUND_PROXY_PORT')
76
-        ) {
77
-            return;
78
-        }
79
-
80
-        $proxy = Environment::getEnv('SS_OUTBOUND_PROXY');
81
-        $proxyPort = Environment::getEnv('SS_OUTBOUND_PROXY_PORT');
82
-
83
-        Environment::setEnv('http_proxy', $proxy . ':' . $proxyPort);
84
-        Environment::setEnv('https_proxy', $proxy . ':' . $proxyPort);
85
-    }
86
-
87
-    /**
88
-     * Configure any domains that should be excluded from egress proxy rules and provide them to the environment
89
-     */
90
-    protected function configureProxyDomainExclusions()
91
-    {
92
-        $noProxy = $this->config()->get('egress_proxy_exclude_domains');
93
-        if (empty($noProxy)) {
94
-            return;
95
-        }
96
-
97
-        if (!is_array($noProxy)) {
98
-            $noProxy = [$noProxy];
99
-        }
100
-
101
-        // Merge with exsiting if needed.
102
-        if (Environment::getEnv('NO_PROXY')) {
103
-            $noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY')), $noProxy);
104
-        }
105
-
106
-        Environment::setEnv('NO_PROXY', implode(',', array_unique($noProxy)));
107
-    }
59
+		$this->configureProxyDomainExclusions();
60
+
61
+		if ($this->config()->get('xss_protection_enabled') && $response) {
62
+			$response->addHeader('X-XSS-Protection', '1; mode=block');
63
+		}
64
+
65
+		return $response;
66
+	}
67
+
68
+	/**
69
+	 * If the outbound egress proxy details have been defined in environment variables, configure the proxy
70
+	 * variables that are used to configure it.
71
+	 */
72
+	protected function configureEgressProxy()
73
+	{
74
+		if (!Environment::getEnv('SS_OUTBOUND_PROXY')
75
+			|| !Environment::getEnv('SS_OUTBOUND_PROXY_PORT')
76
+		) {
77
+			return;
78
+		}
79
+
80
+		$proxy = Environment::getEnv('SS_OUTBOUND_PROXY');
81
+		$proxyPort = Environment::getEnv('SS_OUTBOUND_PROXY_PORT');
82
+
83
+		Environment::setEnv('http_proxy', $proxy . ':' . $proxyPort);
84
+		Environment::setEnv('https_proxy', $proxy . ':' . $proxyPort);
85
+	}
86
+
87
+	/**
88
+	 * Configure any domains that should be excluded from egress proxy rules and provide them to the environment
89
+	 */
90
+	protected function configureProxyDomainExclusions()
91
+	{
92
+		$noProxy = $this->config()->get('egress_proxy_exclude_domains');
93
+		if (empty($noProxy)) {
94
+			return;
95
+		}
96
+
97
+		if (!is_array($noProxy)) {
98
+			$noProxy = [$noProxy];
99
+		}
100
+
101
+		// Merge with exsiting if needed.
102
+		if (Environment::getEnv('NO_PROXY')) {
103
+			$noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY')), $noProxy);
104
+		}
105
+
106
+		Environment::setEnv('NO_PROXY', implode(',', array_unique($noProxy)));
107
+	}
108 108
 }
Please login to merge, or discard this patch.
src/Control/CwpBasicAuthMiddleware.php 1 patch
Indentation   +75 added lines, -75 removed lines patch added patch discarded remove patch
@@ -8,86 +8,86 @@
 block discarded – undo
8 8
 
9 9
 class CwpBasicAuthMiddleware extends BasicAuthMiddleware implements PermissionProvider
10 10
 {
11
-    /**
12
-     * Whitelisted IP addresses will not be given a basic authentication prompt when other basic authentication
13
-     * rules via {@link BasicAuthMiddleware} are enabled.
14
-     *
15
-     * Please note that this will not have any effect if using BasicAuth.entire_site_protected, which will
16
-     * always enabled basic authentication for the entire site.
17
-     *
18
-     * @var array
19
-     */
20
-    protected $whitelistedIps = [];
11
+	/**
12
+	 * Whitelisted IP addresses will not be given a basic authentication prompt when other basic authentication
13
+	 * rules via {@link BasicAuthMiddleware} are enabled.
14
+	 *
15
+	 * Please note that this will not have any effect if using BasicAuth.entire_site_protected, which will
16
+	 * always enabled basic authentication for the entire site.
17
+	 *
18
+	 * @var array
19
+	 */
20
+	protected $whitelistedIps = [];
21 21
 
22
-    /**
23
-     * @return array
24
-     */
25
-    public function getWhitelistedIps()
26
-    {
27
-        return $this->whitelistedIps;
28
-    }
22
+	/**
23
+	 * @return array
24
+	 */
25
+	public function getWhitelistedIps()
26
+	{
27
+		return $this->whitelistedIps;
28
+	}
29 29
 
30
-    /**
31
-     * @param string|string[] $whitelistedIps An array of IP addresses, or a comma delimited string
32
-     * @return $this
33
-     */
34
-    public function setWhitelistedIps($whitelistedIps)
35
-    {
36
-        if (is_string($whitelistedIps)) {
37
-            $whitelistedIps = explode(',', $whitelistedIps);
38
-        }
39
-        $this->whitelistedIps = $whitelistedIps;
40
-        return $this;
41
-    }
30
+	/**
31
+	 * @param string|string[] $whitelistedIps An array of IP addresses, or a comma delimited string
32
+	 * @return $this
33
+	 */
34
+	public function setWhitelistedIps($whitelistedIps)
35
+	{
36
+		if (is_string($whitelistedIps)) {
37
+			$whitelistedIps = explode(',', $whitelistedIps);
38
+		}
39
+		$this->whitelistedIps = $whitelistedIps;
40
+		return $this;
41
+	}
42 42
 
43
-    /**
44
-     * Check for any whitelisted IP addresses. If one matches the current user's IP then return false early,
45
-     * otherwise allow the default {@link BasicAuthMiddleware} to continue its logic.
46
-     *
47
-     * {@inheritDoc}
48
-     */
49
-    protected function checkMatchingURL(HTTPRequest $request)
50
-    {
51
-        if ($this->ipMatchesWhitelist()) {
52
-            return false;
53
-        }
54
-        return parent::checkMatchingURL($request);
55
-    }
43
+	/**
44
+	 * Check for any whitelisted IP addresses. If one matches the current user's IP then return false early,
45
+	 * otherwise allow the default {@link BasicAuthMiddleware} to continue its logic.
46
+	 *
47
+	 * {@inheritDoc}
48
+	 */
49
+	protected function checkMatchingURL(HTTPRequest $request)
50
+	{
51
+		if ($this->ipMatchesWhitelist()) {
52
+			return false;
53
+		}
54
+		return parent::checkMatchingURL($request);
55
+	}
56 56
 
57
-    /**
58
-     * Check whether the current user's IP address is in the IP whitelist
59
-     *
60
-     * @return bool
61
-     */
62
-    protected function ipMatchesWhitelist()
63
-    {
64
-        $whitelist = $this->getWhitelistedIps();
65
-        // Continue if no whitelist is defined
66
-        if (empty($whitelist)) {
67
-            return false;
68
-        }
57
+	/**
58
+	 * Check whether the current user's IP address is in the IP whitelist
59
+	 *
60
+	 * @return bool
61
+	 */
62
+	protected function ipMatchesWhitelist()
63
+	{
64
+		$whitelist = $this->getWhitelistedIps();
65
+		// Continue if no whitelist is defined
66
+		if (empty($whitelist)) {
67
+			return false;
68
+		}
69 69
 
70
-        $userIp = $_SERVER['REMOTE_ADDR'];
71
-        if (in_array($userIp, $whitelist)) {
72
-            return true;
73
-        }
70
+		$userIp = $_SERVER['REMOTE_ADDR'];
71
+		if (in_array($userIp, $whitelist)) {
72
+			return true;
73
+		}
74 74
 
75
-        return false;
76
-    }
75
+		return false;
76
+	}
77 77
 
78
-    /**
79
-     * Provide a permission code for users to be able to access the site in test mode (UAT sites). This will
80
-     * apply to any route other than those required to change your password.
81
-     *
82
-     * @return array
83
-     */
84
-    public function providePermissions()
85
-    {
86
-        return [
87
-            'ACCESS_UAT_SERVER' => _t(
88
-                __CLASS__ . '.UatServerPermission',
89
-                'Allow users to use their accounts to access the UAT server'
90
-            )
91
-        ];
92
-    }
78
+	/**
79
+	 * Provide a permission code for users to be able to access the site in test mode (UAT sites). This will
80
+	 * apply to any route other than those required to change your password.
81
+	 *
82
+	 * @return array
83
+	 */
84
+	public function providePermissions()
85
+	{
86
+		return [
87
+			'ACCESS_UAT_SERVER' => _t(
88
+				__CLASS__ . '.UatServerPermission',
89
+				'Allow users to use their accounts to access the UAT server'
90
+			)
91
+		];
92
+	}
93 93
 }
Please login to merge, or discard this patch.
tests/Control/CwpBasicAuthMiddlewareTest.php 2 patches
Indentation   +88 added lines, -88 removed lines patch added patch discarded remove patch
@@ -13,92 +13,92 @@
 block discarded – undo
13 13
 
14 14
 class CwpBasicAuthMiddlewareTest extends SapphireTest
15 15
 {
16
-    /**
17
-     * @var CwpBasicAuthMiddleware
18
-     */
19
-    protected $middleware;
20
-
21
-    /**
22
-     * @var array
23
-     */
24
-    protected $originalServersVars = [];
25
-
26
-    protected function setUp()
27
-    {
28
-        parent::setUp();
29
-
30
-        $this->middleware = Injector::inst()->get(BasicAuthMiddleware::class);
31
-        $this->originalServersVars = $_SERVER;
32
-
33
-        Config::modify()->set(BasicAuth::class, 'ignore_cli', false);
34
-    }
35
-
36
-    protected function tearDown()
37
-    {
38
-        $_SERVER = $this->originalServersVars;
39
-
40
-        parent::tearDown();
41
-    }
42
-
43
-    public function testSetWhitelistedIps()
44
-    {
45
-        $this->middleware->setWhitelistedIps('127.0.0.1,127.0.0.2');
46
-        $this->assertSame([
47
-            '127.0.0.1',
48
-            '127.0.0.2',
49
-        ], $this->middleware->getWhitelistedIps(), 'Accepts comma delimited strings');
50
-
51
-        $this->middleware->setWhitelistedIps(['127.0.0.1']);
52
-        $this->assertSame(['127.0.0.1'], $this->middleware->getWhitelistedIps(), 'Accepts array values');
53
-    }
54
-
55
-    /**
56
-     * @param string $currentIp
57
-     * @param int $expected
58
-     * @dataProvider whitelistingProvider
59
-     */
60
-    public function testIpWhitelisting($currentIp, $expected)
61
-    {
62
-        // Enable basic auth everywhere
63
-        $this->middleware->setURLPatterns(['#.*#' => true]);
64
-
65
-        // Set a whitelisted IP address
66
-        $_SERVER['REMOTE_ADDR'] = $currentIp;
67
-        $this->middleware->setWhitelistedIps(['127.0.0.1']);
68
-
69
-        $response = $this->mockRequest();
70
-
71
-        $this->assertEquals($expected, $response->getStatusCode());
72
-    }
73
-
74
-    /**
75
-     * @return array[]
76
-     */
77
-    public function whitelistingProvider()
78
-    {
79
-        return [
80
-            'IP not in whitelist' => ['123.456.789.012', 401],
81
-            'IP in whitelist' => ['127.0.0.1', 200],
82
-        ];
83
-    }
84
-
85
-    public function testMiddlewareProvidesUatServerPermissions()
86
-    {
87
-        $this->assertArrayHasKey('ACCESS_UAT_SERVER', $this->middleware->providePermissions());
88
-    }
89
-
90
-    /**
91
-     * Perform a mock middleware request. Will return 200 if everything is OK.
92
-     *
93
-     * @param string $url
94
-     * @return HTTPResponse
95
-     */
96
-    protected function mockRequest($url = '/foo')
97
-    {
98
-        $request = new HTTPRequest('GET', $url);
99
-
100
-        return $this->middleware->process($request, function () {
101
-            return new HTTPResponse('OK', 200);
102
-        });
103
-    }
16
+	/**
17
+	 * @var CwpBasicAuthMiddleware
18
+	 */
19
+	protected $middleware;
20
+
21
+	/**
22
+	 * @var array
23
+	 */
24
+	protected $originalServersVars = [];
25
+
26
+	protected function setUp()
27
+	{
28
+		parent::setUp();
29
+
30
+		$this->middleware = Injector::inst()->get(BasicAuthMiddleware::class);
31
+		$this->originalServersVars = $_SERVER;
32
+
33
+		Config::modify()->set(BasicAuth::class, 'ignore_cli', false);
34
+	}
35
+
36
+	protected function tearDown()
37
+	{
38
+		$_SERVER = $this->originalServersVars;
39
+
40
+		parent::tearDown();
41
+	}
42
+
43
+	public function testSetWhitelistedIps()
44
+	{
45
+		$this->middleware->setWhitelistedIps('127.0.0.1,127.0.0.2');
46
+		$this->assertSame([
47
+			'127.0.0.1',
48
+			'127.0.0.2',
49
+		], $this->middleware->getWhitelistedIps(), 'Accepts comma delimited strings');
50
+
51
+		$this->middleware->setWhitelistedIps(['127.0.0.1']);
52
+		$this->assertSame(['127.0.0.1'], $this->middleware->getWhitelistedIps(), 'Accepts array values');
53
+	}
54
+
55
+	/**
56
+	 * @param string $currentIp
57
+	 * @param int $expected
58
+	 * @dataProvider whitelistingProvider
59
+	 */
60
+	public function testIpWhitelisting($currentIp, $expected)
61
+	{
62
+		// Enable basic auth everywhere
63
+		$this->middleware->setURLPatterns(['#.*#' => true]);
64
+
65
+		// Set a whitelisted IP address
66
+		$_SERVER['REMOTE_ADDR'] = $currentIp;
67
+		$this->middleware->setWhitelistedIps(['127.0.0.1']);
68
+
69
+		$response = $this->mockRequest();
70
+
71
+		$this->assertEquals($expected, $response->getStatusCode());
72
+	}
73
+
74
+	/**
75
+	 * @return array[]
76
+	 */
77
+	public function whitelistingProvider()
78
+	{
79
+		return [
80
+			'IP not in whitelist' => ['123.456.789.012', 401],
81
+			'IP in whitelist' => ['127.0.0.1', 200],
82
+		];
83
+	}
84
+
85
+	public function testMiddlewareProvidesUatServerPermissions()
86
+	{
87
+		$this->assertArrayHasKey('ACCESS_UAT_SERVER', $this->middleware->providePermissions());
88
+	}
89
+
90
+	/**
91
+	 * Perform a mock middleware request. Will return 200 if everything is OK.
92
+	 *
93
+	 * @param string $url
94
+	 * @return HTTPResponse
95
+	 */
96
+	protected function mockRequest($url = '/foo')
97
+	{
98
+		$request = new HTTPRequest('GET', $url);
99
+
100
+		return $this->middleware->process($request, function () {
101
+			return new HTTPResponse('OK', 200);
102
+		});
103
+	}
104 104
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -97,7 +97,7 @@
 block discarded – undo
97 97
     {
98 98
         $request = new HTTPRequest('GET', $url);
99 99
 
100
-        return $this->middleware->process($request, function () {
100
+        return $this->middleware->process($request, function() {
101 101
             return new HTTPResponse('OK', 200);
102 102
         });
103 103
     }
Please login to merge, or discard this patch.