diff --git a/src/Endpoints/FirewallSettings.php b/src/Endpoints/FirewallSettings.php new file mode 100644 index 0000000..36e49fd --- /dev/null +++ b/src/Endpoints/FirewallSettings.php @@ -0,0 +1,135 @@ +adapter = $adapter; + } + + /** + * Get the Security Level feature for a zone. + * + * @param string $zoneID The ID of the zone + * @return string|false + */ + public function getSecurityLevelSetting(string $zoneID) + { + $return = $this->adapter->get( + 'zones/' . $zoneID . '/settings/security_level' + ); + $body = json_decode($return->getBody()); + if (isset($body->result)) { + return $body->result->value; + } + return false; + } + + /** + * Get the Challenge TTL feature for a zone. + * + * @param string $zoneID The ID of the zone + * @return integer|false + */ + public function getChallengeTTLSetting(string $zoneID) + { + $return = $this->adapter->get( + 'zones/' . $zoneID . '/settings/challenge_ttl' + ); + $body = json_decode($return->getBody()); + if (isset($body->result)) { + return $body->result->value; + } + return false; + } + + /** + * Get the Browser Integrity Check feature for a zone. + * + * @param string $zoneID The ID of the zone + * @return string|false + */ + public function getBrowserIntegrityCheckSetting(string $zoneID) + { + $return = $this->adapter->get( + 'zones/' . $zoneID . '/settings/browser_check' + ); + $body = json_decode($return->getBody()); + if (isset($body->result)) { + return $body->result->value; + } + return false; + } + + /** + * Update the Security Level setting for the zone + * + * @param string $zoneID The ID of the zone + * @param string $value The value of the zone setting + * @return bool + */ + public function updateSecurityLevelSetting(string $zoneID, string $value) + { + $return = $this->adapter->patch( + 'zones/' . $zoneID . '/settings/security_level', + [ + 'value' => $value, + ] + ); + $body = json_decode($return->getBody()); + if (isset($body->success) && $body->success == true) { + return true; + } + return false; + } + + /** + * Update the Challenge TTL setting for the zone + * + * @param string $zoneID The ID of the zone + * @param int $value The value of the zone setting + * @return bool + */ + public function updateChallengeTTLSetting(string $zoneID, int $value) + { + $return = $this->adapter->patch( + 'zones/' . $zoneID . '/settings/challenge_ttl', + [ + 'value' => $value, + ] + ); + $body = json_decode($return->getBody()); + if (isset($body->success) && $body->success == true) { + return true; + } + return false; + } + + /** + * Update the Browser Integrity Check setting for the zone + * + * @param string $zoneID The ID of the zone + * @param string $value The value of the zone setting + * @return bool + */ + public function updateBrowserIntegrityCheckSetting(string $zoneID, string $value) + { + $return = $this->adapter->patch( + 'zones/' . $zoneID . '/settings/browser_check', + [ + 'value' => $value, + ] + ); + $body = json_decode($return->getBody()); + if (isset($body->success) && $body->success == true) { + return true; + } + return false; + } +} diff --git a/tests/Endpoints/FirewallSettingsTest.php b/tests/Endpoints/FirewallSettingsTest.php new file mode 100644 index 0000000..b8bc289 --- /dev/null +++ b/tests/Endpoints/FirewallSettingsTest.php @@ -0,0 +1,121 @@ +getPsr7JsonResponseForFixture('Endpoints/getSecurityLevelSetting.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/security_level') + ); + + $firewallSettingsMock = new \Cloudflare\API\Endpoints\FirewallSettings($mock); + $result = $firewallSettingsMock->getSecurityLevelSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); + + $this->assertEquals('medium', $result); + } + + public function testGetChallengeTTLSetting() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/getChallengeTTLSetting.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/challenge_ttl') + ); + + $firewallSettingsMock = new \Cloudflare\API\Endpoints\FirewallSettings($mock); + $result = $firewallSettingsMock->getChallengeTTLSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); + + $this->assertEquals(1800, $result); + } + + public function testGetBrowserIntegrityCheckSetting() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/getBrowserIntegrityCheckSetting.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/browser_check') + ); + + $firewallSettingsMock = new \Cloudflare\API\Endpoints\FirewallSettings($mock); + $result = $firewallSettingsMock->getBrowserIntegrityCheckSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); + + $this->assertEquals('on', $result); + } + + public function testUpdateSecurityLevelSetting() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateSecurityLevelSetting.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('patch')->willReturn($response); + + $mock->expects($this->once()) + ->method('patch') + ->with( + $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/security_level'), + $this->equalTo(['value' => 'medium']) + ); + + $firewallSettingsMock = new \Cloudflare\API\Endpoints\FirewallSettings($mock); + $result = $firewallSettingsMock->updateSecurityLevelSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 'medium'); + + $this->assertTrue($result); + } + + public function testUpdateChallengeTTLSetting() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateChallengeTTLSetting.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('patch')->willReturn($response); + + $mock->expects($this->once()) + ->method('patch') + ->with( + $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/challenge_ttl'), + $this->equalTo(['value' => 1800]) + ); + + $firewallSettingsMock = new \Cloudflare\API\Endpoints\FirewallSettings($mock); + $result = $firewallSettingsMock->updateChallengeTTLSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 1800); + + $this->assertTrue($result); + } + + public function testUpdateBrowserIntegrityCheckSetting() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateBrowserIntegrityCheckSetting.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('patch')->willReturn($response); + + $mock->expects($this->once()) + ->method('patch') + ->with( + $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/browser_check'), + $this->equalTo(['value' => 'on']) + ); + + $firewallSettingsMock = new \Cloudflare\API\Endpoints\FirewallSettings($mock); + $result = $firewallSettingsMock->updateBrowserIntegrityCheckSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 'on'); + + $this->assertTrue($result); + } +} diff --git a/tests/Fixtures/Endpoints/getBrowserIntegrityCheckSetting.json b/tests/Fixtures/Endpoints/getBrowserIntegrityCheckSetting.json new file mode 100644 index 0000000..cdfe4ee --- /dev/null +++ b/tests/Fixtures/Endpoints/getBrowserIntegrityCheckSetting.json @@ -0,0 +1,11 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "browser_check", + "value": "on", + "editable": true, + "modified_on": "2014-01-01T05:20:00.12345Z" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/getChallengeTTLSetting.json b/tests/Fixtures/Endpoints/getChallengeTTLSetting.json new file mode 100644 index 0000000..ee6edf1 --- /dev/null +++ b/tests/Fixtures/Endpoints/getChallengeTTLSetting.json @@ -0,0 +1,11 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "challenge_ttl", + "value": 1800, + "editable": true, + "modified_on": "2014-01-01T05:20:00.12345Z" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/getSecurityLevelSetting.json b/tests/Fixtures/Endpoints/getSecurityLevelSetting.json new file mode 100644 index 0000000..bbbf241 --- /dev/null +++ b/tests/Fixtures/Endpoints/getSecurityLevelSetting.json @@ -0,0 +1,11 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "security_level", + "value": "medium", + "editable": true, + "modified_on": "2014-01-01T05:20:00.12345Z" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/updateBrowserIntegrityCheckSetting.json b/tests/Fixtures/Endpoints/updateBrowserIntegrityCheckSetting.json new file mode 100644 index 0000000..cdfe4ee --- /dev/null +++ b/tests/Fixtures/Endpoints/updateBrowserIntegrityCheckSetting.json @@ -0,0 +1,11 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "browser_check", + "value": "on", + "editable": true, + "modified_on": "2014-01-01T05:20:00.12345Z" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/updateChallengeTTLSetting.json b/tests/Fixtures/Endpoints/updateChallengeTTLSetting.json new file mode 100644 index 0000000..ee6edf1 --- /dev/null +++ b/tests/Fixtures/Endpoints/updateChallengeTTLSetting.json @@ -0,0 +1,11 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "challenge_ttl", + "value": 1800, + "editable": true, + "modified_on": "2014-01-01T05:20:00.12345Z" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/updateSecurityLevelSetting.json b/tests/Fixtures/Endpoints/updateSecurityLevelSetting.json new file mode 100644 index 0000000..bbbf241 --- /dev/null +++ b/tests/Fixtures/Endpoints/updateSecurityLevelSetting.json @@ -0,0 +1,11 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "security_level", + "value": "medium", + "editable": true, + "modified_on": "2014-01-01T05:20:00.12345Z" + } +} \ No newline at end of file