Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jpatokal committed Oct 23, 2023
2 parents e36b643 + f53e452 commit a569d94
Show file tree
Hide file tree
Showing 25 changed files with 416 additions and 228 deletions.
2 changes: 2 additions & 0 deletions help/resetpw.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
echo _("Invalid challenge.");
}
} else {
// TODO: Internationalize
echo "No such user.";
}
} elseif (isset($_POST["email"])) {
Expand All @@ -56,6 +57,7 @@
$name = $row['name'];
$link = "https://openflights.org/help/resetpw?user=" . $name
. "&challenge=" . $row['challenge'];
// TODO: Internationalize? Ideally using users locale...
$subject = "OpenFlights: Reset password";
$body = "Somebody has requested a password reset for your OpenFlights.org account '$name'. To proceed, please click on the link below:
Expand Down
73 changes: 43 additions & 30 deletions php/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

include_once 'greatcircle.php';

// must be a string or locale may turn this into a comma!
/**
* @note Must be a string or locale may turn this into a comma!
*/
const KM_PER_MILE = "1.609344";

/**
Expand All @@ -11,9 +13,10 @@
$KMPERMILE = KM_PER_MILE;

/**
* Mapping of modes from shorthand
*
* @TODO: Internationalise? differing case messages exist
*
* Mapping of modes from shorthand
* @var array
*/
const MODES = [
Expand All @@ -29,9 +32,10 @@
$modes = MODES;

/**
* Mapping of mode operators from shorthand
*
* @TODO: Internationalise?
*
* Mapping of mode operators from shorthand
* @var array
*/
const MODES_OPERATOR = [
Expand All @@ -47,7 +51,8 @@
$modeOperators = MODES_OPERATOR;

/**
* End with JSON-formatted data, localized message and a successful status
* End with JSON-formatted data, localized message and a successful status.
*
* @param $data array
*/
function json_success($data) {
Expand All @@ -57,7 +62,8 @@ function json_success($data) {
}

/**
* Abort with a JSON-formatted localized error message
* Abort with a JSON-formatted localized error message.
*
* @param $msg string
* @param $detail string
*/
Expand All @@ -74,8 +80,9 @@ function json_error($msg, $detail = '') {
}

/**
* Standard formatting of airport data
* @param $row array associative array containing iata, icao
* Standard formatting of airport data.
*
* @param $row array associative array containing IATA and ICAO codes
* @return string " code : apid : x : y : timezone : dstrule "
*/
function format_apdata($row) {
Expand All @@ -91,17 +98,18 @@ function format_apdata($row) {
}

/**
* // Standard formatting of airport codes
* @param $row array associative array containing iata, icao
* Standard formatting of airport codes.
*
* @param $row array associative array containing IATA and ICAO codes
* @return string
*/
function format_apcode($row) {
return format_apcode2($row["iata"], $row["icao"]);
}

/**
* @param $iata string
* @param $icao string
* @param $iata string IATA code
* @param $icao string ICAO code
* @return string
*/
function format_apcode2($iata, $icao) {
Expand All @@ -116,8 +124,9 @@ function format_apcode2($iata, $icao) {
}

/**
* Standard formatting of airport names
* @param $row array associative array containing name, city, country/code and iata/icao
* Standard formatting of airport names.
*
* @param $row array associative array containing name, city, country/code and IATA/ICAO codes
* @return string
*/
function format_airport($row) {
Expand Down Expand Up @@ -145,9 +154,9 @@ function format_airport($row) {
}

/**
* Standard formatting of airline names
* Standard formatting of airline names.
*
* @param $row array associative array containing name, iata, icao and (optionally) mode
* @param $row array associative array containing name, IATA/ICAO codes, and optionally the mode
* @return string
*/
function format_airline($row) {
Expand Down Expand Up @@ -181,12 +190,12 @@ function format_alcode($iata, $icao, $mode) {
}

/**
* Calculate (distance, duration) between two airport IDs
* Calculate (distance, duration) between two Airport IDs.
*
* @param $dbh PDO OpenFlights DB handler
* @param $src_apid string Source APID
* @param $dst_apid string Destination APID
* @return array Distance, duration
* @param $src_apid string Source Airport ID
* @param $dst_apid string Destination Airport ID
* @return array [ distance, duration ]
*/
function gcDistance($dbh, $src_apid, $dst_apid) {
// Special case: loop flight to/from the same airport
Expand Down Expand Up @@ -214,7 +223,7 @@ function gcDistance($dbh, $src_apid, $dst_apid) {
}

/**
* @param $dist
* @param $dist float|int
* @return string
*/
function gcDuration($dist) {
Expand All @@ -224,8 +233,9 @@ function gcDuration($dist) {

/**
* Convert a filename (relative to the document root) to a relative URL with a date-based version string appended.
*
* @param $filename string|null Relative filename (e.g. "/js/foo.js")
* @return string Relative filename with version (e.g. "/js/foo.js?version=20120102")
* @return string Relative filename with a version parameter (e.g. "/js/foo.js?version=20120102")
* @throws Exception Invalid input
*/
function fileUrlWithDate($filename) {
Expand All @@ -246,9 +256,10 @@ function fileUrlWithDate($filename) {
}

/**
* Hack to record X-Y and Y-X flights as same in DB
* @param $src_apid
* @param $dst_apid
* Hack to record X-Y and Y-X flights as same in DB.
*
* @param $src_apid string Source Airport ID
* @param $dst_apid string Destination Airport ID
* @return array
*/
function orderAirports($src_apid, $dst_apid) {
Expand All @@ -260,9 +271,10 @@ function orderAirports($src_apid, $dst_apid) {
}

/**
* if $cond then echo $value
* @param $cond bool
* @param $value string
* if $cond then echo $value.
*
* @param $cond bool Whether to echo $value
* @param $value string Value to be echo-ed
*/
function condOut($cond, $value) {
if ($cond) {
Expand All @@ -271,11 +283,12 @@ function condOut($cond, $value) {
}

/**
* if $arr[$key] == $value then echo $true else echo $false
* if $arr[$key] == $value then echo $true else echo $false.
*
* @param $arr array
* @param $key string
* @param $value mixed
* @param $true string
* @param $true string String to output when $arr[$key] == $value
* @param $false string Default ''
*/
function condArrOut($arr, $key, $value, $true, $false = '') {
Expand All @@ -287,7 +300,7 @@ function condArrOut($arr, $key, $value, $true, $false = '') {
/**
* @param $src mixed
* @param $dst mixed
* @param $flip bool
* @param $flip bool Whether to swap $dst and $src
* @return array
*/
function flip($src, $dst, $flip) {
Expand Down
5 changes: 4 additions & 1 deletion php/locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
$locale = $_SESSION["locale"];
}

if ($locale === null) {
if ($locale === null || $locale === '') {
// https://github.com/jpatokal/openflights/issues/1322
// If we've not already got a locale set (from $_SESSION or $_GET),
// see if we can use the HTTP ACCEPT_LANGUAGE header value
Expand All @@ -30,6 +30,9 @@
// We know we can do en_US, so don't do a DB lookup
if ($acceptLang === "en_US" || locale_exists($dbh, $acceptLang)) {
$locale = $acceptLang;
} else {
// Fallback incase we don't have a locale at this point
$locale = "en_US";
}
}

Expand Down
13 changes: 5 additions & 8 deletions test/client/AirportSearchTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

include_once dirname(__FILE__) . '/OpenFlightsSeleniumTestCase.php';

class AirportSearchTest extends OpenFlightsSeleniumTestCase
{
public function testAnonAirportSearch()
{
class AirportSearchTest extends OpenFlightsSeleniumTestCase {
public function testAnonAirportSearch() {
global $airport;

$this->open("/html/apsearch");
Expand All @@ -21,8 +20,7 @@ public function testAnonAirportSearch()
$this->assertValue("airport", $airport['name']);
}

public function testAnonAirportSearchPreload()
{
public function testAnonAirportSearchPreload() {
global $airport;

$apid = db_apid(db_connect());
Expand All @@ -42,8 +40,7 @@ public function testAnonAirportSearchPreload()
$this->assertValue("iata", "");
}

public function testLoggedInAirportEdit()
{
public function testLoggedInAirportEdit() {
global $settings, $airport;

$this->open('/');
Expand Down
6 changes: 2 additions & 4 deletions test/client/OpenFlightsSeleniumTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

include_once dirname(__FILE__) . '/../server/config.php';

class OpenFlightsSeleniumTestCase extends Selenium2TestCase
{
class OpenFlightsSeleniumTestCase extends Selenium2TestCase {
protected $captureScreenshotOnFailure = true;
protected $screenshotPath = '/tmp/screenshots';
protected $screenshotUrl = '/tmp/screenshots';

protected function setUp(): void
{
protected function setUp(): void {
$this->setBrowser('*firefox /Applications/Firefox.app/Contents/MacOS/firefox-bin');
$this->setBrowserUrl('http://openflights.local/');
$this->setSpeed(100);
Expand Down
13 changes: 5 additions & 8 deletions test/client/WebLoginTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

include_once dirname(__FILE__) . '/OpenFlightsSeleniumTestCase.php';

class WebLoginTest extends OpenFlightsSeleniumTestCase
{
public function testFailedLogin()
{
class WebLoginTest extends OpenFlightsSeleniumTestCase {
public function testFailedLogin() {
global $settings;

$this->open('/');
Expand All @@ -28,8 +27,7 @@ public function testFailedLogin()
$this->click('close');
}

public function testSuccessfulLogin()
{
public function testSuccessfulLogin() {
global $settings;

$this->open('/');
Expand All @@ -45,8 +43,7 @@ public function testSuccessfulLogin()
$this->verifyTextPresent("Add new flights");
}

public function testReload()
{
public function testReload() {
global $settings;

$this->open('/');
Expand Down
9 changes: 4 additions & 5 deletions test/client/WebMapPublicTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

include_once dirname(__FILE__) . '/OpenFlightsSeleniumTestCase.php';

class WebMapPublicTest extends OpenFlightsSeleniumTestCase
{
public function testPublicUserMapAsAnonymous()
{
class WebMapPublicTest extends OpenFlightsSeleniumTestCase {
public function testPublicUserMapAsAnonymous() {
global $settings;

$this->open("/user/" . $settings['name']);
Expand All @@ -24,7 +23,7 @@ public function testPublicUserMapAsAnonymous()
$this->click('link=Decatur Aviation');
$this->verifyTextPresent($settings['name'] . "'s flights on Decatur Aviation");

# Back to main map
# Back to the main map
$this->select('Airlines', 'label=All carriers');
$this->verifyTextPresent($settings['name'] . "'s flights");
}
Expand Down
2 changes: 1 addition & 1 deletion test/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This test suite validates PHP calls and MySQL database content.
1. `php composer.phar install`
2. Configure `config.php`
3. Run `php settings.php` to populate test user
4. Then run eg. `php login.php` to test login cases, _or_
4. Then run e.g. `php login.php` to test login cases, _or_
5. Load `/test/server/test_all.php` in a browser to test all.

NOTE: Many cases are dependent on each other. Generally, run settings.php first, all cases afterward, and cleanup.php last.
Loading

0 comments on commit a569d94

Please sign in to comment.