diff --git a/PHPCI/Controller/BuildStatusController.php b/PHPCI/Controller/BuildStatusController.php index d7ccac2f..56f82011 100644 --- a/PHPCI/Controller/BuildStatusController.php +++ b/PHPCI/Controller/BuildStatusController.php @@ -60,40 +60,6 @@ class BuildStatusController extends \PHPCI\Controller $this->shieldsClient = $shieldsClient; } - /** - * Returns status of the last build - * - * @param int $projectId - * - * @return string - * - * @throws Exception - */ - protected function getStatus($projectId) - { - $branch = $this->getParam('branch', 'master'); - try { - $project = $this->projectStore->getById($projectId); - $status = 'passing'; - - if (!$project->getAllowPublicStatus()) { - return null; - } - - if (isset($project) && $project instanceof Project) { - $build = $project->getLatestBuild($branch, array(2,3)); - - if (isset($build) && $build instanceof Build && $build->getStatus() != 2) { - $status = 'failed'; - } - } - } catch (Exception $e) { - $status = 'error'; - } - - return $status; - } - /** * Displays projects information in ccmenu format * @@ -138,18 +104,6 @@ class BuildStatusController extends \PHPCI\Controller $xml = new \SimpleXMLElement(''); } - return $this->renderXml($xml); - } - - /** - * Render the XML object - * - * @param \SimpleXMLElement $xml - * - * @return Response - */ - protected function renderXml(\SimpleXMLElement $xml = null) - { $this->response->disableLayout(); $this->response->setHeader('Content-Type', 'text/xml'); $this->response->setContent($xml->asXML()); @@ -166,16 +120,20 @@ class BuildStatusController extends \PHPCI\Controller */ public function image($projectId) { + $project = $this->projectStore->getById($projectId); + + if (empty($project)) { + throw new NotFoundException(Lang::get('project_x_not_found', $projectId)); + } + + if (!$project->getAllowPublicStatus()) { + throw new NotAuthorizedException(); + } + $style = $this->getParam('style', 'plastic'); $label = $this->getParam('label', 'build'); - - $status = $this->getStatus($projectId); - - if (is_null($status)) { - $response = new b8\Http\Response\RedirectResponse(); - $response->setHeader('Location', '/'); - return $response; - } + $branch = $this->getParam('branch', 'master'); + $status = $this->getStatus($project, $branch); $color = ($status == 'passing') ? 'green' : 'red'; $image = $this->shieldsClient->get( @@ -235,4 +193,28 @@ class BuildStatusController extends \PHPCI\Controller return $builds['items']; } + + /** + * Returns status of the last build + * + * @param int $projectId + * + * @return string + * + * @throws Exception + */ + protected function getStatus($project, $branch) + { + try { + $build = $project->getLatestBuild($branch, array(2,3)); + + if (isset($build) && $build instanceof Build && $build->getStatus() != 2) { + return 'failed'; + } + } catch (Exception $e) { + return 'error'; + } + + return 'passing'; + } } diff --git a/Tests/PHPCI/Controller/BuildStatusControllerTest.php b/Tests/PHPCI/Controller/BuildStatusControllerTest.php index a8332e8e..05d9dc8c 100644 --- a/Tests/PHPCI/Controller/BuildStatusControllerTest.php +++ b/Tests/PHPCI/Controller/BuildStatusControllerTest.php @@ -69,4 +69,63 @@ class BuildStatusControllerTest extends \PHPUnit_Framework_TestCase $this->assertEquals('text/xml', $responseData['headers']['Content-Type']); $this->assertXmlStringEqualsXmlString('', $responseData['body']); } + + + /** + * @expectedException \b8\Exception\HttpException\NotAuthorizedException + */ + public function test_image_hidden_for_non_public_project() + { + $buildStore = $this->prophesize('PHPCI\Store\BuildStore'); + $projectStore = $this->prophesize('PHPCI\Store\ProjectStore'); + + $project = new Project(); + $project->setAllowPublicStatus(false); + + $projectStore->getById(Argument::any())->willReturn($project); + + $webController = new BuildStatusController( + $this->prophesize('PHPCI\Config')->reveal(), + $this->prophesize('b8\Http\Request')->reveal(), + new \b8\Http\Response(), + $buildStore->reveal(), + $projectStore->reveal(), + $this->prophesize('b8\HttpClient')->reveal() + ); + + $result = $webController->handleAction('image', [1]); + } + + public function test_image_visible_for_public_project() + { + $buildStore = $this->prophesize('PHPCI\Store\BuildStore'); + $projectStore = $this->prophesize('PHPCI\Store\ProjectStore'); + $project = new Project(); + $project->setId(1); + $project->setBranch('test'); + $project->setAllowPublicStatus(true); + + $projectStore->getById(1)->willReturn($project); + + $shieldsClient = $this->prophesize('b8\HttpClient'); + $shieldsClient->get(Argument::any(), Argument::any())->willReturn(array( + 'body' => '', + )); + + $webController = new BuildStatusController( + $this->prophesize('PHPCI\Config')->reveal(), + $this->prophesize('b8\Http\Request')->reveal(), + new \b8\Http\Response(), + $buildStore->reveal(), + $projectStore->reveal(), + $shieldsClient->reveal() + ); + + $result = $webController->handleAction('image', [1]); + $this->assertInstanceOf('b8\Http\Response', $result); + + $responseData = $result->getData(); + $this->assertEquals('image/svg+xml', $responseData['headers']['Content-Type']); + $this->assertXmlStringEqualsXmlString('', $responseData['body']); + } }