Esempio n. 1
0
 /**
  * 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());
 }
Esempio n. 3
0
     //preform the a put post or delete
     if ($method === "PUT") {
         //verify the xsrf token
         $segment = Segment::getSegmentBySegmentId($pdo, $id);
         // verify the segment in question exists
         if ($segment === null) {
             throw new RuntimeException("segment must exist", 404);
         }
         $segment = new Segment($id, $segmentStart, $segmentStop, $requestObject->segmentStartElevation, $requestObject->segmentStopElevation);
         $segment->update($pdo);
         $reply->message = "segment update was successful";
     }
     if ($method === "POST") {
         // form a mini-constructor to assemble a segmentStart and a segmentStop....?????
         $segment = new Segment(null, $segmentStart, $segmentStop, $requestObject->segmentStartElevation, $requestObject->segmentStopElevation);
         $segment->insert($pdo);
         $reply->message = "segment insert was successful";
     }
 } elseif ($method === "DELETE") {
     $segment = Segment::getSegmentBySegmentId($pdo, $id);
     if ($segment === null) {
         throw new RuntimeException("segment must exist", 404);
     }
     $segment->delete($pdo);
     $deletedObject = new stdClass();
     $deletedObject->segmentId = $id;
     $reply->message = "segment was successfully Deleted";
 } else {
     if (empty($method) === false && $method !== "GET") {
         throw new RuntimeException("only active users are allowed to modify entries", 401);
     }
Esempio n. 4
0
 /**
  *
  * Decodes geoJson file, converts to string, sifts through the string and inserts the data into the database
  *
  * @param string $url
  * @throws PDOException PDO related errors
  * @throws Exception catch-all exception
  **/
 public static function readTrailSegmentsGeoJson($url)
 {
     $context = stream_context_create(array("http" => array("ignore_errors" => true, "method" => "GET")));
     try {
         $pdo = connectToEncryptedMySQL("/var/www/trailquail/encrypted-mysql/trailquail.ini");
         if (($jsonData = file_get_contents($url, null, $context)) !== false) {
             if (($jsonFd = @fopen("php://memory", "wb+")) === false) {
                 throw new RuntimeException("Memory Error: I can't remember");
             }
             //decode the geoJson file
             $jsonConverted = json_decode($jsonData);
             $jsonFeatures = $jsonConverted->features;
             // create array from converted geoJson file
             $properties = new SplFixedArray(count($jsonFeatures));
             //loop through array to get to json properties
             foreach ($jsonFeatures as $jsonFeature) {
                 $properties[$properties->key()] = $jsonFeature->properties;
                 $properties->next();
             }
             //create an array of trails and a SplObjectStorage for the trail guide
             $trails = [];
             $trailGuide = new SplObjectStorage();
             //loop through array to get trail name and trail use
             foreach ($properties as $property) {
                 $trailName = $property->name;
                 $trailUse = "";
                 //set $trailUse string based on information in geoJson properties field.
                 if ($property->bicycle === "yes") {
                     $trailUse = $trailUse . "bicycle: yes, ";
                 } else {
                     $trailUse = $trailUse . "bicycle: no, ";
                 }
                 if ($property->foot === "yes") {
                     $trailUse = $trailUse . "foot: yes, ";
                 } else {
                     $trailUse = $trailUse . "foot: no, ";
                 }
                 if ($property->wheelchair === "yes") {
                     $trailUse = $trailUse . "wheelchair: yes ";
                 } else {
                     $trailUse = $trailUse . "wheelchair: no ";
                 }
                 //get the trail by name and set the trail use
                 try {
                     $candidateTrails = Trail::getTrailByTrailName($pdo, $trailName);
                     foreach ($candidateTrails as $trail) {
                         $trail->setTrailUse($trailUse);
                         $trail->update($pdo);
                         $trails[] = $trail;
                     }
                 } catch (PDOException $pdoException) {
                     $sqlStateCode = "23000";
                     $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);
                 }
             }
             try {
                 $trailIndex = 0;
                 foreach ($jsonFeatures as $jsonFeature) {
                     $jsonCoordinates = $jsonFeature->geometry->coordinates;
                     if ($jsonFeature->geometry->type === "LineString") {
                         $coordinates = new SplFixedArray(count($jsonCoordinates));
                         foreach ($jsonCoordinates as $coordinate) {
                             $coordinates[$coordinates->key()] = $coordinate;
                             $coordinates->next();
                         }
                         $trailGuide[$trails[$trailIndex]] = $coordinates;
                         $trailIndex++;
                     } else {
                         if ($jsonFeature->geometry->type === "MultiLineString") {
                             $trailClones = [];
                             $trails[$trailIndex]->delete($pdo);
                             for ($i = 1; $i <= count($jsonCoordinates); $i++) {
                                 $trail = clone $trails[$trailIndex];
                                 $trail->setTrailId(null);
                                 $trail->setTrailName($trail->getTrailName() . " {$i}");
                                 $trail->insert($pdo);
                                 $trailClones[] = $trail;
                             }
                             array_splice($trails, $trailIndex, 1, $trailClones);
                             foreach ($jsonCoordinates as $lineCoordinates) {
                                 $trailGuide[$trails[$trailIndex]] = $lineCoordinates;
                                 $trailIndex++;
                             }
                         }
                     }
                 }
                 $trailGuide->rewind();
                 foreach ($trailGuide as $map) {
                     $trail = $trailGuide->current();
                     $geo = $trailGuide->getInfo();
                     for ($indexTwo = 0; $indexTwo < count($geo) - 1; $indexTwo++) {
                         $segmentStartX = $geo[$indexTwo][0];
                         $segmentStartY = $geo[$indexTwo][1];
                         //							$segmentStartElevation = $geo[$indexTwo][2];
                         $segmentStopX = $geo[$indexTwo + 1][0];
                         $segmentStopY = $geo[$indexTwo + 1][1];
                         //							$segmentStopElevation = $geo[$indexTwo + 1][2];
                         $segmentStart = new Point($segmentStartX, $segmentStartY);
                         $segmentStop = new Point($segmentStopX, $segmentStopY);
                         try {
                             $segment = new Segment(null, $segmentStart, $segmentStop, 0, 0);
                             $segment->insert($pdo);
                             $relationship = new TrailRelationship($segment->getSegmentId(), $trail->getTrailId(), "T");
                             $relationship->insert($pdo);
                         } catch (PDOException $pdoException) {
                             $sqlStateCode = "23000";
                             $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);
                         }
                     }
                 }
             } catch (PDOException $pdoException) {
                 $sqlStateCode = "23000";
                 $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($jsonFd);
     } catch (PDOException $pdoException) {
         throw new PDOException($pdoException->getMessage(), 0, $pdoException);
     } catch (Exception $exception) {
         throw new Exception($exception->getMessage(), 0, $exception);
     }
 }
Esempio n. 5
0
 /**
  * test grabbing a segment by SegmentStopElevation
  **/
 public function testGetValidSegmentByStopElevation()
 {
     //count the number of rows and save it for later
     $numRows = $this->getConnection()->getRowCount("segment");
     //create a new segment and insert it into the database
     $segment = new Segment(null, $this->VALID_SEGMENTSTART, $this->VALID_SEGMENTSTOP, $this->VALID_SEGMENTSTARTELEVATION, $this->VALID_SEGMENTSTOPELEVATION);
     $segment->insert($this->getPDO());
     //grab the data from mySQL and verify the fields
     $pdoSegments = Segment::getSegmentBySegmentStopElevation($this->getPDO(), $segment->getSegmentStopElevation());
     $this->assertSame($numRows + 1, $this->getConnection()->getRowCount("segment"));
     foreach ($pdoSegments as $pdoSegment) {
         $this->assertSame($pdoSegment->getSegmentStart()->getX(), $this->VALID_SEGMENTSTART->getX());
         $this->assertSame($pdoSegment->getSegmentStart()->getY(), $this->VALID_SEGMENTSTART->getY());
         $this->assertSame($pdoSegment->getSegmentStop()->getX(), $this->VALID_SEGMENTSTOP->getX());
         $this->assertSame($pdoSegment->getSegmentStop()->getY(), $this->VALID_SEGMENTSTOP->getY());
         $this->assertSame($pdoSegment->getSegmentStartElevation(), $this->VALID_SEGMENTSTARTELEVATION);
         $this->assertSame($pdoSegment->getSegmentStopElevation(), $this->VALID_SEGMENTSTOPELEVATION);
     }
 }