/** * This setUp function changes the date string to a DateTime object, creates a segment, creates test values for user salt and hash, and then creates user and trail entries to use for testing */ public function setUp() { parent::setUp(); // necessary DateTime format to run the test $this->VALID_CREATEDATE = DateTime::createFromFormat("Y-m-d H:i:s", $this->VALID_CREATEDATE); //create points needed for segment $segmentStart = new Point(35.554, 44.546); $segmentStop = new Point(35.554, 48.445); //create new segment to use for testing $this->segment = new Segment(null, $segmentStart, $segmentStop, 7565, 9800); $this->segment->insert($this->getPDO()); //create needed dependencies to ensure user can be created to run unit testing $this->salt = bin2hex(openssl_random_pseudo_bytes(32)); $this->hash = hash_pbkdf2("sha512", "iLoveIllinois", $this->salt, 262144, 128); //create a new user to use for testing $this->user = new User(null, $this->VALID_BROWSER, $this->VALID_CREATEDATE, $this->VALID_IPADDRESS, "S", "*****@*****.**", $this->hash, "george kephart", $this->salt); $this->user->insert($this->getPDO()); // create a trail to own test // Trail(trailId, userId, browser, createDate, ipAddress, submitTrailId, trailAccessibility, trailAmenities, trailConditions, $this->trail = new Trail(null, $this->user->getUserId(), "Safari", $this->VALID_CREATEDATE, $this->VALID_IPADDRESS, null, "y", "Picnic area", "Good", "This trail is a beautiful winding trail located in the Sandia Mountains", 3, 1054.53, "la luz trail", 1, "Mostly switchbacks with a few sections of rock fall", "Heavy", "Hiking", "fpfyRTmt6XeE9ehEKZ5LwF"); $this->trail->insert($this->getPDO()); }
/** * create dependent objects before running each test **/ public function setUp() { //run the default setUp() method first parent::setUp(); $this->VALID_DATE = DateTime::createFromFormat("Y-m-d H:i:s", $this->VALID_DATE); //create browser $this->VALID_BROWSER = "Chrome"; $this->VALID_USERSALT = bin2hex(openssl_random_pseudo_bytes(32)); $this->VALID_USERHASH = $this->VALID_USERHASH = hash_pbkdf2("sha512", "password4321", $this->VALID_USERSALT, 262144, 128); //create and insert a userId to own the trail $this->user = new User(null, $this->VALID_BROWSER, $this->VALID_DATE, "192.168.1.168", "S", "*****@*****.**", $this->VALID_USERHASH, "Hyourname.tomorrow", $this->VALID_USERSALT); $this->user->insert($this->getPDO()); $this->VALID_TRAILNAME = "La Luz"; //create and insert a trailId to own the test Trail Relationship //$newTrailId, $newUserId, $newBrowser, $newCreateDate, $newIpAddress, $newSubmitTrailId, $newTrailAccessibility, $newTrailAmenities, $newTrailCondition, $newTrailDescription, $newTrailDifficulty, $newTrailDistance, $newTrailName, $newTrailSubmissionType, $newTrailTerrain, $newTrailTraffic, $newTrailUse, $newTrailUuid $this->trail = new Trail(null, $this->user->getUserId(), "Safari", $this->VALID_DATE, "192.168.1.4", null, "y", "Picnic area", "Good", "This trail is a beautiful winding trail located in the Sandia Mountains", 3, 1054.53, $this->VALID_TRAILNAME, 1, "Mostly switchbacks with a few sections of rock fall", "Heavy", "Hiking", "SSEERFFV4444554"); $this->trail->insert($this->getPDO()); $this->segmentStart = new Point(35.554, 44.546); $this->segmentStop = new Point(6, 36); //create and insert a segmentId to own the test Trail Relationship $this->segment = new Segment(null, $this->segmentStart, $this->segmentStop, 1000, 2000); $this->segment->insert($this->getPDO()); }
/** *This function grabs the named_trails.csv file and reads it * * @param string $url url to grab file at * @throws PDOException PDO related errors * @throws Exception catch-all exception **/ public static function readNamedTrailsCSV($url) { $context = stream_context_create(array("http" => array("ignore_errors" => true, "method" => "GET"))); try { $pdo = connectToEncryptedMySQL("/var/www/trailquail/encrypted-mysql/trailquail.ini"); if (($fd = @fopen($url, "rb", false, $context)) !== false) { fgetcsv($fd, 0, ","); while (($data = fgetcsv($fd, 0, ",", "\"")) !== false && feof($fd) !== true) { $trailId = null; $userId = 1; $browser = "Default"; $createDate = new DateTime(); $ipAddress = "::1"; $submitTrailId = null; $trailAmenities = "This trail currently has no amenities information. If you are familiar with this trail, please use the submission form to help us out! Thank you."; $trailCondition = "This trail currently has no condition information. If you are familiar with this trail, please use the submission form to help us out! Thank you."; $trailDescription = ""; $trailDifficulty = 2; $trailDistance = 0; $trailName = $data[1]; $trailSubmissionType = 2; $trailTerrain = "Unknown"; $trailTraffic = "Unknown"; $trailUse = "Unknown"; $trailUuid = null; //sets trail description based on available data. if (strlen($data[3]) <= 0) { $trailDescription = "This trail currently has no description information. If you are familiar with this trail, please use the submission form to help us out! Thank you."; } else { $trailDescription = $data[3]; } try { $trail = new Trail(null, $userId, $browser, $createDate, $ipAddress, $submitTrailId, $trailAmenities, $trailCondition, $trailDescription, $trailDifficulty, $trailDistance, $trailName, $trailSubmissionType, $trailTerrain, $trailTraffic, $trailUse, $trailUuid); $trail->insert($pdo); } catch (PDOException $pdoException) { $sqlStateCode = "23000"; echo "I knew there was a catch somewhere :" . $pdoException->getMessage() . PHP_EOL; $errorInfo = $pdoException->errorInfo; if ($errorInfo[0] === $sqlStateCode) { } else { throw new PDOException($pdoException->getMessage(), 0, $pdoException); } } catch (Exception $exception) { throw new Exception($exception->getMessage(), 0, $exception); } } fclose($fd); } } catch (PDOException $pdoException) { throw new PDOException($pdoException->getMessage(), 0, $pdoException); } catch (Exception $exception) { throw new Exception($exception->getMessage(), 0, $exception); } }
} if ($method === "PUT") { verifyXsrf(); $trail = Trail::getTrailById($pdo, $id); if ($trail === null) { throw new RuntimeException("trail does not exist", 404); } $trail = new Trail($id, $requestObject->userId, $trail->getBrowser(), $trail->getCreateDate(), $trail->getIpAddress(), $requestObject->submitTrailId, $requestObject->trailAmenities, $requestObject->trailCondition, $requestObject->trailDescription, $requestObject->trailDifficulty, $requestObject->trailDistance, $requestObject->trailName, $requestObject->trailSubmissionType, $requestObject->trailTerrain, $requestObject->trailTraffic, $requestObject->trailUse, $requestObject->trailUuid); $trail->update($pdo); $reply->message = "trail updated okay"; } if ($method === "POST") { verifyXsrf(); //preform the actual post/do i need to treat foreign keys in any special manner $trail = new Trail(null, $requestObject->userId, $_SERVER["HTTP_USER_AGENT"], new DateTime(), $_SERVER["REMOTE_ADDR"], $requestObject->submitTrailId, $requestObject->trailAmenities, $requestObject->trailCondition, $requestObject->trailDescription, $requestObject->trailDifficulty, $requestObject->trailDistance, $requestObject->trailName, $requestObject->trailSubmissionType, $requestObject->trailTerrain, $requestObject->trailTraffic, $requestObject->trailUse, $requestObject->trailUuid); $trail->insert($pdo); $reply->message = "trail submitted okay"; } } } else { // if not an active user and attempting a method other than get, throw an exception if (empty($method) === false && $method !== "GET") { throw new RuntimeException("only active users are allowed to modify entries", 401); } } } catch (Exception $exception) { $reply->status = $exception->getCode(); $reply->message = $exception->getMessage(); //blob } header("Content-type: application/json");
/** * test grabbing a trail by TrailUuId **/ public function testGetValidTrailByTrailUuId() { //count the number of rows and save it for later $numRows = $this->getConnection()->getRowCount("trail"); //create a new trail and insert it into mySQL $trail = new Trail(null, $this->user->getUserId(), $this->VALID_BROWSER, $this->VALID_CREATEDATE, $this->VALID_IPADDRESS, $this->VALID_SUBMITTRAILID, $this->VALID_TRAILAMENITIES, $this->VALID_TRAILCONDITIION, $this->VALID_TRAILDESCRIPTION, $this->VALID_TRAILDIFFICULTY, $this->VALID_TRAILDISTANCE, $this->VALID_TRAILNAME, $this->VALID_TRAILSUBMISSIONTYPE, $this->VALID_TRAILTERRAIN, $this->VALID_TRAILTRAFFIC, $this->VALID_TRAILUSE, $this->VALID_TRAILUUID); $trail->insert($this->getPDO()); //grab the data from mySQL and enforce the fields match our expectations $pdoTrails = Trail::getTrailByTrailUuId($this->getPDO(), $trail->getTrailUuId()); foreach ($pdoTrails as $pdoTrail) { $this->assertSame($numRows + 1, $this->getConnection()->getRowCount("trail")); $this->assertLessThan($pdoTrail->getTrailId(), 0); $this->assertSame($pdoTrail->getUserId(), $this->user->getUserId()); $this->assertSame($pdoTrail->getBrowser(), $this->VALID_BROWSER); $this->assertEquals($pdoTrail->getCreateDate(), $this->VALID_CREATEDATE); $this->assertSame($pdoTrail->getIpAddress(), $this->VALID_IPADDRESS); $this->assertSame($pdoTrail->getSubmitTrailId(), $this->VALID_SUBMITTRAILID); $this->assertSame($pdoTrail->getTrailAmenities(), $this->VALID_TRAILAMENITIES); $this->assertSame($pdoTrail->getTrailCondition(), $this->VALID_TRAILCONDITIION); $this->assertSame($pdoTrail->getTrailDescription(), $this->VALID_TRAILDESCRIPTION); $this->assertSame($pdoTrail->getTrailDifficulty(), $this->VALID_TRAILDIFFICULTY); $this->assertSame($pdoTrail->getTrailDistance(), $this->VALID_TRAILDISTANCE); $this->assertSame($pdoTrail->getTrailName(), $this->VALID_TRAILNAME); $this->assertSame($pdoTrail->getTrailDescription(), $this->VALID_TRAILDESCRIPTION); $this->assertSame($pdoTrail->getTrailSubmissionType(), $this->VALID_TRAILSUBMISSIONTYPE); $this->assertSame($pdoTrail->getTrailTerrain(), $this->VALID_TRAILTERRAIN); $this->assertSame($pdoTrail->getTrailTraffic(), $this->VALID_TRAILTRAFFIC); $this->assertSame($pdoTrail->getTrailUse(), $this->VALID_TRAILUSE); $this->assertSame($this->longUuidToShortUuid($pdoTrail->getTrailUuId()), $this->VALID_TRAILUUID); } }