/** * Test grabbing image tags by tagId */ public function testGetImageTagByTagId() { //Count the number of rows and save it for later $numRows = $this->getConnection()->getRowCount("imageTag"); //Create new image and insert into database $imageTag = new ImageTag($this->imageTagImage->getImageId(), $this->imageTagTag->getTagId()); $imageTag->insert($this->getPDO()); //Get data from database and ensure the fields match our expectations $pdoImageTag = ImageTag::getImageTagByTagId($this->getPDO(), $this->imageTagTag->getTagId()); $this->assertEquals($numRows + 1, $this->getConnection()->getRowCount("imageTag")); $this->assertContainsOnlyInstancesOf("Edu\\Cnm\\Jpegery\\ImageTag", $pdoImageTag); //Grabs results from array and validate it $imageTagResults = $pdoImageTag[0]; $this->assertEquals($imageTagResults->getTagId(), $this->imageTagTag->getTagId()); $this->assertEquals($imageTagResults->getImageId(), $this->imageTagImage->getImageId()); }
/** * test grabbing all tags **/ public function testGetAllValidTags() { //count the number of rows and save it for later $numRows = $this->getConnection()->getRowCount("tag"); //create a new Tag and insert it into mySQL $tag = new Tag(null, $this->VALID_TAGNAME); $tag->insert($this->getPDO()); //grab the data from mySQL and enforce the fields match our expectations $results = Tag::getAllTags($this->getPDO()); $this->assertEquals($numRows + 1, $this->getConnection()->getRowCount("tag")); $this->assertCount(1, $results); $this->assertContainsOnlyInstancesOf("Edu\\Cnm\\Jpegery\\Tag", $results); //grab the result from the array and validate it $pdoTag = $results[0]; $this->assertEquals($pdoTag->getTagId(), $tag->getTagId()); $this->assertEquals($pdoTag->getTagName(), $this->VALID_TAGNAME); }
if (empty($_SESSION["profile"]) !== false) { if ($method === "POST") { verifyXsrf(); $requestContent = file_get_contents("php://input"); $requestObject = json_decode($requestContent); } //ensure all fields are present if (empty($requestObject->imageId) === true) { throw new InvalidArgumentException("Image must have an ID", 405); } if (empty($requestObject->tagId) === true) { throw new InvalidArgumentException("Tag must have an ID", 405); } //perform actual POST or DELETE if ($method === "POST") { $tag = new Tag($requestObject->imageId, $requestObject->tagName); $tag->insert($pdo); } elseif ($method === "DELETE") { $image = imageTag::getImageTagByImageIdAndTagId($pdo, $id); if ($image === null) { throw new RuntimeException("Image does not exist", 404); } $security = $image->getImageProfileId(); if ($security !== $_SESSION["profile"]->getProfileId()) { throw new RuntimeException("You cannot edit an image that is not yours.", 403); } $image = Image::getImageByImageId($pdo, $id); if ($image === null) { throw new RuntimeException("Image does not exist", 404); } $image->delete($pdo);
} //handle REST calls for PUT methods //If the user is logged in, allow to POST their own tag. if (empty($_SESSION["profile"]) !== false) { if ($method === "POST") { verifyXsrf(); $requestContent = file_get_contents("php://input"); $requestObject = json_decode($requestContent); } //ensure all fields are present if (empty($requestObject->tagId) === true) { throw new InvalidArgumentException("Tag must have an ID", 405); } if (empty($requestObject->tagName) === true) { throw new InvalidArgumentException("Tag must have a Name", 405); } if ($method === "POST") { $tag = new Tag(null, $requestObject->tagName); $tag->insert($pdo); $reply->imageId = $tag->getTagId(); } } } catch (Exception $exception) { $reply->status = $exception->getCode(); $reply->message = $exception->getMessage(); } header("Content-type: application/json"); if ($reply->data === null) { unset($reply->data); } echo json_encode($reply);