From e4d8158f6cba9a32a1ad09ac8ba67e7c8f085e4a Mon Sep 17 00:00:00 2001 From: iturgeon Date: Sat, 10 Jun 2017 22:19:36 -0400 Subject: [PATCH] adds phpcs for enforcing syntax rules and updates code to meet it --- .travis.yml | 3 +- bin/db_create_tables.php | 18 ++- bin/move_reports_to_db.php | 53 ++++---- composer.json | 23 ++-- composer.lock | 151 ++++++++++++++++++---- config/herokuConfig.php | 6 +- config/localConfig.template.php | 4 +- config/localConfig.test.php | 6 +- config/settings.php | 10 +- config/tests.php | 62 ++++----- lib/Udoit.php | 92 ++++++++------ lib/UdoitDB.php | 44 +++++-- lib/UdoitJob.php | 85 ++++++++----- lib/UdoitUtils.php | 111 +++++++++++------ lib/Ufixit.php | 87 +++++++------ lib/worker.php | 11 +- phpcs.xml | 129 +++++++++++++++++++ public/assets/js/default.js | 2 +- public/cached.php | 46 +++---- public/index.php | 44 +++---- public/oauth2response.php | 90 ++++++------- public/parsePdf.php | 24 ++-- public/parsePdfProgress.php | 24 ++-- public/parseResults.php | 60 ++++----- public/process.php | 11 +- public/progress.php | 121 +++++++++--------- public/scanner.php | 42 +++---- tests/BaseTest.php | 57 +++++---- tests/FunctionalTests.php | 6 +- tests/IndexTest.php | 11 +- tests/MockObj.php | 31 +++++ tests/PDOMock.php | 77 ++++++++++++ tests/PDOStatementMock.php | 61 +++++++++ tests/UdoitDBTest.php | 123 +++++------------- tests/UdoitJobTest.php | 49 +++++--- tests/UdoitTest.php | 215 +++++++++++++++++--------------- tests/UfixitTest.php | 55 +++++--- tests/bootstrap.php | 1 + 38 files changed, 1266 insertions(+), 779 deletions(-) create mode 100644 phpcs.xml create mode 100644 tests/MockObj.php create mode 100644 tests/PDOMock.php create mode 100644 tests/PDOStatementMock.php diff --git a/.travis.yml b/.travis.yml index b175cc15e..eea5440e7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,11 +4,12 @@ cache: - $COMPOSER_CACHE_DIR - vendor install: - - 'composer install --no-scripts' + - 'composer install' php: - '5.4' - '5.5' - '5.6' script: - 'composer lint' + = 'composer sniff-summary' - 'composer test' diff --git a/bin/db_create_tables.php b/bin/db_create_tables.php index 2c7bb910d..7cb592a6f 100644 --- a/bin/db_create_tables.php +++ b/bin/db_create_tables.php @@ -1,8 +1,7 @@ query("SELECT * FROM {$db_reports_table}")->fetchAll(); -if( ! isset($rows[0]['file_path'])) -{ - exit("It looks like this script doesnt need to be run"); +if (!isset($rows[0]['file_path'])) { + exit("It looks like this script doesnt need to be run"); } -if( ! isset($rows[0]['report_json'])) -{ - // Quick hack to add report column since we dont have migrations yet - $column_type = $db_type == 'mysql' ? 'MEDIUMTEXT' : 'TEXT'; - $dbh->query("ALTER TABLE {$db_reports_table} ADD report_json {$column_type}"); +if (!isset($rows[0]['report_json'])) { + // Quick hack to add report column since we dont have migrations yet + $column_type = $db_type === 'mysql' ? 'MEDIUMTEXT' : 'TEXT'; + $dbh->query("ALTER TABLE {$db_reports_table} ADD report_json {$column_type}"); } $sth = $dbh->prepare("UPDATE {$db_reports_table} set report_json = :report_json WHERE id = :id"); $count_moved = 0; -foreach ($rows as $row) -{ - if(empty($row['file_path'])) continue; +foreach ($rows as $row) { + if (empty($row['file_path'])) { + continue; + } - $file = __DIR__. '/'. $row['file_path']; - if( ! file_exists($file)){ - echo("Json file not found {$file} for report id: {$row['id']}\n"); - continue; - } + $file = __DIR__."/{$row['file_path']}"; + if (!file_exists($file)) { + echo("Json file not found {$file} for report id: {$row['id']}\n"); + continue; + } - $json = file_get_contents($file); + $json = file_get_contents($file); - if(empty($json)) continue; + if (empty($json)) { + continue; + } - $sth->bindValue(':report_json', $json, PDO::PARAM_STR); - $sth->bindValue(':id', $row['id'], PDO::PARAM_INT); - $res = $sth->execute(); + $sth->bindValue(':report_json', $json, PDO::PARAM_STR); + $sth->bindValue(':id', $row['id'], PDO::PARAM_INT); + $res = $sth->execute(); - if(!$res){ - echo("Failed inserting report for {$row['id']}"); - continue; - } + if (!$res) { + echo("Failed inserting report for {$row['id']}"); + continue; + } - $count_moved++; + ++$count_moved; } $dbh->query("ALTER TABLE {$db_reports_table} DROP COLUMN file_path"); diff --git a/composer.json b/composer.json index 3a835ed33..0b68a04c3 100755 --- a/composer.json +++ b/composer.json @@ -7,17 +7,20 @@ "license": "GPL-3.0", "scripts":{ "test": "vendor/phpunit/phpunit/phpunit --exclude-group functional --coverage-html ./reports --coverage-text", - "testall": "vendor/phpunit/phpunit/phpunit", - "lint": "find . -type f -iname \"*.php\" -not -path \"./lib/quail/tests/*\" -not -path \"./vendor/*\" | xargs -n1 php -l", - "dbsetup" : "php bin/db_create_tables.php", + "test-all": "vendor/phpunit/phpunit/phpunit", + "lint": "./vendor/bin/parallel-lint --exclude vendor --exclude lib/quail .s", + "sniff": "./vendor/bin/phpcs --extensions=php .", + "sniff-errors-only": "./vendor/bin/phpcs -n --extensions=php .", + "sniff-summary": "./vendor/bin/phpcs --report=summary --extensions=php .", + "db-setup" : "php bin/db_create_tables.php", "migrate" : "php bin/move_reports_to_db.php", "start": "php -S localhost:8000 -t public/", - "test:docker": "docker-compose run --rm web php composer.phar test", - "testall:docker": "docker-compose run --rm web php composer.phar testall", - "lint:docker": "docker-compose run --rm web php composer.phar lint", - "dbsetup:docker": "docker-compose run --rm web php composer.phar dbsetup", - "start:docker": "docker-compose run --rm web php -S localhost:8000 -t public/" + "test-docker": "docker-compose run --rm web php composer.phar test", + "test-all-docker": "docker-compose run --rm web php composer.phar testall", + "lint-docker": "docker-compose run --rm web php composer.phar lint", + "db-setup-docker": "docker-compose run --rm web php composer.phar dbsetup", + "start-docker": "docker-compose run --rm web php -S localhost:8000 -t public/" }, "repositories" : [ { @@ -55,7 +58,9 @@ "symfony/yaml": "v2.8.9", "heroku/heroku-buildpack-php": "v121", "mockery/mockery": "^0.9.9", - "mikey179/vfsStream": "^1.6" + "jakub-onderka/php-parallel-lint": "^0.9.2", + "squizlabs/php_codesniffer": "^3.0", + "escapestudios/symfony2-coding-standard": "3.x-dev" }, "autoload": { "psr-0": { diff --git a/composer.lock b/composer.lock index 674d5c4b2..cee7e6546 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "c75f1908eebeb53dd01e65a109375a8f", + "content-hash": "a8317c2b598a8a91db236839c609dba6", "packages": [ { "name": "EastDesire/jscolor", @@ -511,6 +511,53 @@ ], "time": "2015-06-14T21:17:01+00:00" }, + { + "name": "escapestudios/symfony2-coding-standard", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/djoos/Symfony-coding-standard.git", + "reference": "03f63512cac06e6ccf5694c503121aba978fdd55" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/djoos/Symfony-coding-standard/zipball/03f63512cac06e6ccf5694c503121aba978fdd55", + "reference": "03f63512cac06e6ccf5694c503121aba978fdd55", + "shasum": "" + }, + "require": { + "squizlabs/php_codesniffer": "3.*" + }, + "type": "phpcodesniffer-standard", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "David Joos", + "email": "iam@davidjoos.com" + }, + { + "name": "Community contributors", + "homepage": "https://github.com/djoos/Symfony2-coding-standard/graphs/contributors" + } + ], + "description": "CodeSniffer ruleset for the Symfony 2+ coding standard", + "homepage": "https://github.com/djoos/Symfony2-coding-standard", + "keywords": [ + "Coding Standard", + "Symfony2", + "phpcs", + "symfony" + ], + "time": "2017-06-08 11:46:24" + }, { "name": "hamcrest/hamcrest-php", "version": "v1.2.2", @@ -601,50 +648,51 @@ "time": "2017-03-27T23:33:27+00:00" }, { - "name": "mikey179/vfsStream", - "version": "v1.6.4", + "name": "jakub-onderka/php-parallel-lint", + "version": "v0.9.2", "source": { "type": "git", - "url": "https://github.com/mikey179/vfsStream.git", - "reference": "0247f57b2245e8ad2e689d7cee754b45fbabd592" + "url": "https://github.com/JakubOnderka/PHP-Parallel-Lint.git", + "reference": "2ead2e4043ab125bee9554f356e0a86742c2d4fa" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/mikey179/vfsStream/zipball/0247f57b2245e8ad2e689d7cee754b45fbabd592", - "reference": "0247f57b2245e8ad2e689d7cee754b45fbabd592", + "url": "https://api.github.com/repos/JakubOnderka/PHP-Parallel-Lint/zipball/2ead2e4043ab125bee9554f356e0a86742c2d4fa", + "reference": "2ead2e4043ab125bee9554f356e0a86742c2d4fa", "shasum": "" }, "require": { - "php": ">=5.3.0" + "php": ">=5.3.3" }, "require-dev": { - "phpunit/phpunit": "~4.5" + "jakub-onderka/php-console-highlighter": "~0.3", + "nette/tester": "~1.3" }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.6.x-dev" - } + "suggest": { + "jakub-onderka/php-console-highlighter": "Highlight syntax in code snippet" }, + "bin": [ + "parallel-lint" + ], + "type": "library", "autoload": { - "psr-0": { - "org\\bovigo\\vfs\\": "src/main/php" - } + "classmap": [ + "./" + ] }, "notification-url": "https://packagist.org/downloads/", "license": [ - "BSD-3-Clause" + "BSD-2-Clause" ], "authors": [ { - "name": "Frank Kleine", - "homepage": "http://frankkleine.de/", - "role": "Developer" + "name": "Jakub Onderka", + "email": "jakub.onderka@gmail.com" } ], - "description": "Virtual file system to mock the real file system in unit tests.", - "homepage": "http://vfs.bovigo.org/", - "time": "2016-07-18T14:02:57+00:00" + "description": "This tool check syntax of PHP files about 20x faster than serial check.", + "homepage": "https://github.com/JakubOnderka/PHP-Parallel-Lint", + "time": "2015-12-15T10:42:16+00:00" }, { "name": "mockery/mockery", @@ -1567,6 +1615,57 @@ "homepage": "https://github.com/sebastianbergmann/version", "time": "2015-06-21T13:59:46+00:00" }, + { + "name": "squizlabs/php_codesniffer", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "b95ff2c3b122a3ee4b57d149a57d2afce65522c3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/b95ff2c3b122a3ee4b57d149a57d2afce65522c3", + "reference": "b95ff2c3b122a3ee4b57d149a57d2afce65522c3", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "bin": [ + "bin/phpcs", + "bin/phpcbf" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "http://www.squizlabs.com/php-codesniffer", + "keywords": [ + "phpcs", + "standards" + ], + "time": "2017-05-04T00:33:04+00:00" + }, { "name": "symfony/yaml", "version": "v2.8.9", @@ -1619,7 +1718,9 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "escapestudios/symfony2-coding-standard": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/config/herokuConfig.php b/config/herokuConfig.php index 26d2b89bb..d8c9553b5 100755 --- a/config/herokuConfig.php +++ b/config/herokuConfig.php @@ -40,14 +40,14 @@ $unscannable_suggestion_on = true; /* Google/YouTube Data Api Key */ -define( 'GOOGLE_API_KEY', getenv('GOOGLE_API_KEY')?:''); +define('GOOGLE_API_KEY', getenv('GOOGLE_API_KEY')?:''); /* Google Analytics Tracking Code */ -define( 'GA_TRACKING_CODE', ''); +define('GA_TRACKING_CODE', ''); // Fix some issues caused by the heroku load balancer // The OAUTH signature verification doesn't know it's using https w/o this -if ( ! empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { +if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { $_SERVER['HTTPS'] = 'on'; } diff --git a/config/localConfig.template.php b/config/localConfig.template.php index 04d2e3d40..793d4cec8 100755 --- a/config/localConfig.template.php +++ b/config/localConfig.template.php @@ -20,10 +20,10 @@ $canvas_nav_item_name = getenv('CANVAS_NAV_ITEM_NAME'); /* Google/YouTube Data Api Key */ -define( 'GOOGLE_API_KEY', ''); +define('GOOGLE_API_KEY', ''); /* Google Analytics Tracking Code */ -define( 'GA_TRACKING_CODE', ''); +define('GA_TRACKING_CODE', ''); /* Database Config */ $db_type = 'mysql'; // 'mysql' or 'pgsql' diff --git a/config/localConfig.test.php b/config/localConfig.test.php index 7950034ba..e7d0fee62 100644 --- a/config/localConfig.test.php +++ b/config/localConfig.test.php @@ -10,16 +10,16 @@ $oauth2_uri = 'test_oauth2_uri'; /* Disable headings check character count */ -$doc_length ='1500'; +$doc_length = '1500'; /* Tool name for display in Canvas Navigation */ $canvas_nav_item_name = 'test udoit'; /* Google/YouTube Data Api Key */ -define( 'GOOGLE_API_KEY', 'TEST_API_KEY'); +define('GOOGLE_API_KEY', 'TEST_API_KEY'); /* Google Analytics Tracking Code */ -define( 'GA_TRACKING_CODE', 'TEST_GA_TRACKING'); +define('GA_TRACKING_CODE', 'TEST_GA_TRACKING'); /* Database Config */ $db_type = 'test'; // 'mysql' or 'pgsql' diff --git a/config/settings.php b/config/settings.php index 609288e48..73bcff365 100755 --- a/config/settings.php +++ b/config/settings.php @@ -13,9 +13,9 @@ // ADD A DEFAULT LOG HANDLER // !! override by creating $log_handler in your config -if ( ! isset($log_handler)) { - $log_handler = new \Monolog\Handler\StreamHandler(__DIR__.'/log.log', \Monolog\Logger::DEBUG); - $log_handler->setFormatter(new \Monolog\Formatter\LineFormatter(null, null, true, true)); +if (!isset($log_handler)) { + $log_handler = new \Monolog\Handler\StreamHandler(__DIR__.'/log.log', \Monolog\Logger::DEBUG); + $log_handler->setFormatter(new \Monolog\Formatter\LineFormatter(null, null, true, true)); } // SET UP LOGGER @@ -37,8 +37,8 @@ UdoitDB::setup($db_type, $dsn, $db_user, $db_password); // BACKGROUND WORKER -if (isset($background_worker_enabled)){ - UdoitJob::$background_worker_enabled = $background_worker_enabled; +if (isset($background_worker_enabled)) { + UdoitJob::$background_worker_enabled = $background_worker_enabled; } // Prevent Caching on the client diff --git a/config/tests.php b/config/tests.php index e3671cfb7..79a5ea49e 100755 --- a/config/tests.php +++ b/config/tests.php @@ -11,13 +11,13 @@ 'resources' => [ 'Canvas Tutorial', 'WCAG Guidelines', - 'WCAG Standard 2.4.4' + 'WCAG Standard 2.4.4', ], 'example' => '

Incorrect

-
'. htmlspecialchars('') .'
+
'.htmlspecialchars('').'

Correct

-
'. htmlspecialchars('read the document') .'
+
'.htmlspecialchars('read the document').'
', ], [ @@ -30,9 +30,9 @@ ], 'example' => '

Incorrect

-
'. htmlspecialchars('') .'
+
'.htmlspecialchars('').'

Correct

-
'. htmlspecialchars('A photograph of a dog') .'
+
'.htmlspecialchars('A photograph of a dog').'
', ], [ @@ -45,11 +45,11 @@ ], 'example' => '

Incorrect

-
'. htmlspecialchars('dog.jpg') .'
-
'. htmlspecialchars('http://website.com/dog.jpg') .'
+
'.htmlspecialchars('dog.jpg').'
+
'.htmlspecialchars('http://website.com/dog.jpg').'

Correct

-
'. htmlspecialchars('A photograph of a dog') .'
-
'. htmlspecialchars('A photograph of a dog') .'
+
'.htmlspecialchars('A photograph of a dog').'
+
'.htmlspecialchars('A photograph of a dog').'
', ], [ @@ -58,13 +58,13 @@ 'desc' => '

Alternative Text (Alt Text) is an alternative (non-visual) way to describe the meaning of an image. Please provide a brief (under 100 characters) description of the image for a screen reader user. Note: It should not be the image file name.

', 'resources' => [ 'Resource on Alternative Text', - 'WCAG Standard: 1.1.1' + 'WCAG Standard: 1.1.1', ], 'example' => '

Incorrect

-
'. htmlspecialchars('I am alt text that is just way too long, look at me being way too long and being a hassle!') .'
+
'.htmlspecialchars('I am alt text that is just way too long, look at me being way too long and being a hassle!').'

Correct

-
'. htmlspecialchars('Short and sweet description') .'
+
'.htmlspecialchars('Short and sweet description').'
', ], [ @@ -73,13 +73,13 @@ 'desc' => '

Alternative Text (Alt Text) is an alternative (non-visual) way to describe the meaning of an image. Please provide a brief (under 100 characters) description of the image for a screen reader user. Note: It should not be the image file name.

', 'resources' => [ 'Resource on Alternative Text', - 'WCAG Standard: 1.1.1' + 'WCAG Standard: 1.1.1', ], 'example' => '

Incorrect

-
'. htmlspecialchars(' ') .'
+
'.htmlspecialchars(' ').'

Correct

-
'. htmlspecialchars('Alt text') .'
+
'.htmlspecialchars('Alt text').'
', ], [ @@ -92,9 +92,9 @@ ], 'example' => '

Incorrect

-
'. htmlspecialchars('
Header OneHeader Two
1.304.50
') .'
+
'.htmlspecialchars('
Header OneHeader Two
1.304.50
').'

Correct

-
'. htmlspecialchars('
Header OneHeader Two
1.304.50
') .'
+
'.htmlspecialchars('
Header OneHeader Two
1.304.50
').'
', ], [ @@ -107,10 +107,10 @@ ], 'example' => '

Incorrect

-
'. htmlspecialchars(''."\n\t".''."\n\t\t".''."\n\t\t".''."\n\t".''."\n\t".''."\n\t\t".''."\n\t\t".''."\n\t".''."\n".'
Heading 1Heading 2
Cell 1Cell 2
') .'
+
'.htmlspecialchars(''."\n\t".''."\n\t\t".''."\n\t\t".''."\n\t".''."\n\t".''."\n\t\t".''."\n\t\t".''."\n\t".''."\n".'
Heading 1Heading 2
Cell 1Cell 2
').'

Correct

-
'. htmlspecialchars(''."\n\t".''."\n\t\t".''."\n\t\t".''."\n\t".''."\n\t".''."\n\t\t".''."\n\t\t".''."\n\t".''."\n".'
Heading 1Heading 2
Cell 1Cell 2
') .'
-
'. htmlspecialchars(''."\n\t".''."\n\t\t".''."\n\t\t".''."\n\t".''."\n\t".''."\n\t\t".''."\n\t\t".''."\n\t".''."\n".'
Heading 1Cell 1
Heading 2Cell 2
') .'
+
'.htmlspecialchars(''."\n\t".''."\n\t\t".''."\n\t\t".''."\n\t".''."\n\t".''."\n\t\t".''."\n\t\t".''."\n\t".''."\n".'
Heading 1Heading 2
Cell 1Cell 2
').'
+
'.htmlspecialchars(''."\n\t".''."\n\t\t".''."\n\t\t".''."\n\t".''."\n\t".''."\n\t\t".''."\n\t\t".''."\n\t".''."\n".'
Heading 1Cell 1
Heading 2Cell 2
').'
', ], [ @@ -137,9 +137,9 @@ ], 'example' => '

Incorrect

-
'. htmlspecialchars('') .'
+
'.htmlspecialchars('').'

Correct

-
'. htmlspecialchars('A widget of stock prices. Access this widget.') .'
+
'.htmlspecialchars('A widget of stock prices. Access this widget.').'
', ], ], @@ -180,20 +180,20 @@ 'resources' => [ 'Canvas Tutorial', 'WCAG Guidelines', - 'WCAG Standard 2.4.4' + 'WCAG Standard 2.4.4', ], 'example' => '

Incorrect

-
'. htmlspecialchars('click here!') .'
+
'.htmlspecialchars('click here!').'

Correct

-
'. htmlspecialchars('read the document') .'
+
'.htmlspecialchars('read the document').'
', ], [ 'name' => 'objectTextUpdatesWhenObjectChanges', 'title' => 'The text equivalents (e.g., transcripts and/or captions) for embedded content should update when content changes.', 'desc' => '', - 'resources' => ['WCAG Standard: 1.2',], + 'resources' => ['WCAG Standard: 1.2'], 'example' => '', ], [ @@ -212,9 +212,9 @@ ], 'example' => '

Incorrect

-
'. htmlspecialchars('

') .'
+
'.htmlspecialchars('

').'

Correct

-
'. htmlspecialchars('

Title

') .'
+
'.htmlspecialchars('

Title

').'
', ], [ @@ -234,9 +234,9 @@ 'resources' => [], 'example' => '

Incorrect

-
'. htmlspecialchars('

Header 1

') .'
+
'.htmlspecialchars('

Header 1

').'

Correct

-
'. htmlspecialchars('

Header 1

') .'
+
'.htmlspecialchars('

Header 1

').'
', ], [ @@ -244,7 +244,7 @@ 'title' => 'Avoid using color alone for emphasis', 'desc' => '

When emphasizing text, you may use color with sufficient contrast as long as you also apply some other form of emphasis, such as bold or italics. This ensures that screen reader users are aware of the text\'s importance.

', 'resources' => [ - 'Resource Link' + 'Resource Link', ], 'example' => '

This example shows how to use the em and strong elements to emphasize text. The em and strong elements were designed to indicate structural emphasis that may be rendered in a variety of ways (font style changes, speech inflection changes, etc.).

diff --git a/lib/Udoit.php b/lib/Udoit.php index 11a0b20ec..a0bce8fc9 100755 --- a/lib/Udoit.php +++ b/lib/Udoit.php @@ -17,19 +17,20 @@ * * Primary Author Contact: Jacob Bates */ -require_once(__DIR__.'/../config/settings.php'); -require_once(__DIR__.'/quail/quail/quail.php'); use Httpful\Request; -class Udoit { +class Udoit +{ /** * Retrieves an entire group of content by type and scans it * @param string api_key - API Key of the user were acting as * @param string content_type - The group of content types we'll retrieve EX: 'pages' or 'assignments' + * * @return array - Results of the scan */ - public static function retrieveAndScan($api_key, $canvas_api_url, $course_id, $content_type){ + public static function retrieveAndScan($api_key, $canvas_api_url, $course_id, $content_type) + { $items_with_issues = []; $totals = ['errors' => 0, 'warnings' => 0, 'suggestions' => 0]; $module_urls = []; @@ -43,8 +44,10 @@ public static function retrieveAndScan($api_key, $canvas_api_url, $course_id, $c $content['amount'] = count($content['items']); // remove results w/o issues and count the totals - foreach ($content['items'] as $item){ - if ($item['amount'] == 0) continue; + foreach ($content['items'] as $item) { + if ($item['amount'] == 0) { + continue; + } $items_with_issues[] = $item; $totals['errors'] += count($item['error']); @@ -53,10 +56,10 @@ public static function retrieveAndScan($api_key, $canvas_api_url, $course_id, $c } // format results - if ($content['module_urls']){ + if ($content['module_urls']) { $module_urls = array_merge($module_urls, $content['module_urls']); } - if ($content['unscannable']){ + if ($content['unscannable']) { $unscannables = array_merge($unscannables, $content['unscannable']); } @@ -69,24 +72,28 @@ public static function retrieveAndScan($api_key, $canvas_api_url, $course_id, $c 'title' => $content_type, 'items' => $items_with_issues, 'amount' => $content['amount'], - 'time' => $content['time'] - ] - ] + 'time' => $content['time'], + ], + ], ]; } /** * Calls the Quail library to generate a UDOIT report * @param array $scanned_content - The items from whatever type of Canvas content was scanned + * * @return array - The report results */ - public static function scanContent(Array $content_items) { + public static function scanContent(array $content_items) + { + require_once(__DIR__.'/quail/quail/quail.php'); $report = []; // Runs each item through the Quail accessibility checker foreach ($content_items as $item) { - - if (empty($item['content'])) continue; + if (empty($item['content'])) { + continue; + } $quail = new quail($item['content'], 'wcag2aaa', 'string', 'static'); $quail->runCheck(); @@ -99,8 +106,9 @@ public static function scanContent(Array $content_items) { // loop over the items returning from Quail foreach ($quail_report['report'] as $quail_issue) { - - if (empty($quail_issue['severity_num'])) continue; + if (empty($quail_issue['severity_num'])) { + continue; + } $issue_count++; @@ -140,9 +148,11 @@ public static function scanContent(Array $content_items) { * @param string $api_key - Api key for the user we're acting as * @param string $canvas_api_url - Base uri for your canvas api * @param string $type - The type of course content to be scanned + * * @return array - The report results */ - public static function getCourseContent($api_key, $canvas_api_url, $course_id, $type) { + public static function getCourseContent($api_key, $canvas_api_url, $course_id, $type) + { global $logger; $api_url = "{$canvas_api_url}/api/v1/courses/{$course_id}/"; @@ -162,7 +172,7 @@ public static function getCourseContent($api_key, $canvas_api_url, $course_id, $ 'id' => $c->id, 'content' => $c->message, 'title' => $c->title, - 'url' => $c->html_url + 'url' => $c->html_url, ]; } break; @@ -174,7 +184,7 @@ public static function getCourseContent($api_key, $canvas_api_url, $course_id, $ 'id' => $c->id, 'content' => $c->description, 'title' => $c->name, - 'url' => $c->html_url + 'url' => $c->html_url, ]; } break; @@ -186,7 +196,7 @@ public static function getCourseContent($api_key, $canvas_api_url, $course_id, $ 'id' => $c->id, 'content' => $c->message, 'title' => $c->title, - 'url' => $c->html_url + 'url' => $c->html_url, ]; } break; @@ -194,31 +204,33 @@ public static function getCourseContent($api_key, $canvas_api_url, $course_id, $ case 'files': $contents = static::apiGetAllLinks($api_key, "{$api_url}files?"); foreach ($contents as $c) { - if (substr($c->display_name, 0, 2) == '._') continue; + if (substr($c->display_name, 0, 2) === '._') { + continue; + } $extension = pathinfo($c->filename, PATHINFO_EXTENSION); - if($c->size > 50000000) { + if ($c->size > 50000000) { $content_result['unscannable'][] = [ 'title' => $c->display_name, 'url' => $c->url, - 'big' => true - ]; + 'big' => true, + ]; } else { if (in_array($extension, ['html', 'htm'])) { $content_result['items'][] = [ 'id' => $c->id, 'content' => Request::get($c->url)->followRedirects()->expectsHtml()->send()->body, 'title' => $c->display_name, - 'url' => $c->url + 'url' => $c->url, ]; } // filters non html files - if (in_array($extension, ['pdf', 'doc', 'docx', 'ppt', 'pptx'])){ + if (in_array($extension, ['pdf', 'doc', 'docx', 'ppt', 'pptx'])) { $content_result['unscannable'][] = [ 'title' => $c->display_name, 'url' => $c->url, - 'big' => false + 'big' => false, ]; } } @@ -235,7 +247,7 @@ public static function getCourseContent($api_key, $canvas_api_url, $course_id, $ 'id' => $wiki_page->body->url, 'content' => $wiki_page->body->body, 'title' => $wiki_page->body->title, - 'url' => $wiki_page->body->html_url + 'url' => $wiki_page->body->html_url, ]; } break; @@ -254,7 +266,7 @@ public static function getCourseContent($api_key, $canvas_api_url, $course_id, $ 'id' => $c->id, 'external_url' => $c->external_url, 'title' => $c->title, - 'url' => $c->html_url + 'url' => $c->html_url, ]; } } @@ -269,7 +281,7 @@ public static function getCourseContent($api_key, $canvas_api_url, $course_id, $ 'id' => $c->id, 'content' => $c->syllabus_body, 'title' => 'Syllabus', - 'url' => "{$canvas_api_url}/courses/{$course_id}/assignments/syllabus" + 'url' => "{$canvas_api_url}/courses/{$course_id}/assignments/syllabus", ]; } break; @@ -279,7 +291,8 @@ public static function getCourseContent($api_key, $canvas_api_url, $course_id, $ } // get every page from the Canvas API till there's none left - protected static function apiGetAllLinks($api_key, $url){ + protected static function apiGetAllLinks($api_key, $url) + { global $logger; $limit = 500; $per_page = 100; @@ -289,13 +302,20 @@ protected static function apiGetAllLinks($api_key, $url){ $req_url = "{$url}page=1&per_page={$per_page}&access_token={$api_key}"; $logger->addInfo("Requesting {$req_url}"); $response = Request::get($req_url)->send(); - if($response->status > 400){ + if ($response->status > 400) { $logger->addError("Error response from {$req_url}"); break; } + $links = static::apiParseLinks($response->headers->toArray()['link']); - foreach ($response->body as $thing) $results[] = $thing; - if (isset($links['next'])) $url = "{$links['next']}&access_token={$api_key}"; + foreach ($response->body as $thing) { + $results[] = $thing; + } + + if (isset($links['next'])) { + $url = "{$links['next']}&access_token={$api_key}"; + } + usleep(250000); // 1/4 sec } while (isset($links['next']) && $cur_page < $limit); @@ -305,9 +325,11 @@ protected static function apiGetAllLinks($api_key, $url){ /** * Parses pagination links returned from Canvas * @param string $links - The pagination links + * * @return array - An array of the separated links */ - protected static function apiParseLinks($links) { + protected static function apiParseLinks($links) + { $links = explode(',', $links); $pretty = []; diff --git a/lib/UdoitDB.php b/lib/UdoitDB.php index 857871aa9..1729cee49 100755 --- a/lib/UdoitDB.php +++ b/lib/UdoitDB.php @@ -18,7 +18,8 @@ * Primary Author Contact: Jacob Bates */ -class UdoitDB{ +class UdoitDB +{ const TEST_EXPIRE_TIME = 5; // 5 seconds, may need to be adjusted @@ -30,7 +31,8 @@ class UdoitDB{ private static $pdo = null; private static $last_test_time = 0; - public static function setup($db_type, $dsn, $db_user, $db_password){ + public static function setup($db_type, $dsn, $db_user, $db_password) + { static::$type = $db_type; static::$user = $db_user; static::$dsn = $dsn; @@ -38,10 +40,11 @@ public static function setup($db_type, $dsn, $db_user, $db_password){ } // acts as a pass through for pdo that checks the connection - public static function __callStatic($name, $args){ + public static function __callStatic($name, $args) + { static::testAndReconnect(); // test to see if the method exists on pdo - if ( ! method_exists(static::$pdo, $name)){ + if (!method_exists(static::$pdo, $name)) { throw new \RuntimeException("{$name} method does not exist on PDO object"); } @@ -49,49 +52,64 @@ public static function __callStatic($name, $args){ return call_user_func_array([static::$pdo, $name], $args); } - public static function testAndReconnect($force_test = false){ + public static function testAndReconnect($force_test = false) + { // make sure we're still connected - if ( ! static::test($force_test)) static::connect(); + if (!static::test($force_test)) { + static::connect(); + } } // runs a very basic query on the db to verify connection status - public static function test($force = false){ - if(empty(static::$pdo)) return false; + public static function test($force = false) + { + if (empty(static::$pdo)) { + return false; + } + try { $now = time(); // check if forced or the last test time expired - if($force || (static::$last_test_time + self::TEST_EXPIRE_TIME) < $now){ + if ($force || (static::$last_test_time + self::TEST_EXPIRE_TIME) < $now) { static::$last_test_time = $now; static::$pdo->query('SELECT version()'); } + return true; } catch (\Exception $e) { error_log("Database Test Connection Error"); error_log($e->getMessage()); + return false; } } - public static function disconnect(){ + public static function disconnect() + { static::$pdo = null; // there's no disconnect w/ pdo, it's cleaned up when garbage collected } - protected static function connect(){ + protected static function connect() + { try { - switch(static::$type) { + switch (static::$type) { case 'test': $db = new PDO('sqlite::memory:'); break; + case 'pgsql': - $db = new static::$dbClass(static::$dsn); + $db = new static::$dbClass(static::$dsn); break; + case 'mysql': $db = new static::$dbClass(static::$dsn, static::$user, static::$password); break; + default: throw new \RuntimeException("Database type ".static::$type." not supported."); break; } + static::$last_test_time = time(); static::$pdo = $db; } catch (\RuntimeException $e) { diff --git a/lib/UdoitJob.php b/lib/UdoitJob.php index ebc0a15ea..09d6227aa 100644 --- a/lib/UdoitJob.php +++ b/lib/UdoitJob.php @@ -1,14 +1,17 @@ execute(); // RUN NOW if the background worker isn't enabled - if ( ! static::$background_worker_enabled){ + if (!static::$background_worker_enabled) { static::runNextJob(); } } - public static function countJobsRemaining(){ + public static function countJobsRemaining() + { $sql = "SELECT COUNT(*) FROM job_queue WHERE status = 'new'"; $query = UdoitDB::query($sql); + return $query ? (int) $query->fetchColumn() : 0; } - public static function runNextJob() { + public static function runNextJob() + { $job_failed = false; try { - if($job = static::getNextJob()) { - if ($job->job_type === 'finalize_report') { + if ($job = static::getNextJob()) { + if ('finalize_report' === $job->job_type) { $result = static::finalizeReport($job); - } - else { + } else { $api_key = UdoitUtils::instance()->getValidRefreshedApiKey($job->user_id); - if(empty($api_key)) throw new Exception("No valid api key for job"); // @TODO: mark job as unrunnable + if (empty($api_key)) { + throw new Exception("No valid api key for job"); // @TODO: mark job as unrunnable + } $job_data = json_decode($job->data, true); $canvas_api_url = $job_data['base_uri']; $result = Udoit::retrieveAndScan($api_key, $canvas_api_url, $job_data['course_id'], $job_data['scan_item']); @@ -54,18 +61,21 @@ public static function runNextJob() { static::finishJobWithResults($job->id, $result); } - } catch(Exception $e) { + } catch (Exception $e) { $job_failed = true; global $logger; $logger->addError($e->getMessage()); $logger->addError(print_r($job, true)); - if($job) self::updateJobStatus($job_record, 'error'); + if ($job) { + self::updateJobStatus($job_record, 'error'); + } } return $job_failed; } - protected static function finalizeReport($job) { + protected static function finalizeReport($job) + { $report = static::combineJobResults($job->job_group); $report['course'] = $job_data['course_title']; $job_data = json_decode($job->data, true); @@ -79,7 +89,9 @@ protected static function finalizeReport($job) { (:userid, :courseid, :report_json, :errors, :suggestions)"; // if using postgres, ask to get the id back since lastInsertId won't work - if(UdoitDB::$type === 'pgsql') $sql .= ' RETURNING id'; + if ('pgsql' === UdoitDB::$type) { + $sql .= ' RETURNING id'; + } $sth = UdoitDB::prepare($sql); $sth->bindValue(':userid', $job->user_id, PDO::PARAM_INT); @@ -88,34 +100,32 @@ protected static function finalizeReport($job) { $sth->bindValue(':errors', $report['total_results']['errors'], PDO::PARAM_STR); $sth->bindValue(':suggestions', $report['total_results']['suggestions'], PDO::PARAM_STR); - if ( ! $sth->execute()) { + if (!$sth->execute()) { error_log(print_r($sth->errorInfo(), true)); throw new Exception('Error inserting report into database'); } - if(UdoitDB::$type === 'pgsql'){ + if ('pgsql' === UdoitDB::$type) { $res = $sth->fetch(PDO::FETCH_ASSOC); $report_id = $res['id']; - - } - else{ + } else { $report_id = UdoitDB::lastInsertId(); } return ['report_id' => $report_id ]; } - protected static function combineJobResults($job_group) { + protected static function combineJobResults($job_group) + { $totals = ['errors' => 0, 'warnings' => 0, 'suggestions' => 0]; - $module_urls = []; + $module_urls = []; $unscannables = []; - $content = []; + $content = []; // combine the data from each job's results $sql = "SELECT * FROM job_queue WHERE job_group = '{$job_group}' AND job_type != 'finalize_report'"; $rows = UdoitDB::query($sql)->fetchAll(); foreach ($rows as $row) { - $results = json_decode($row['results'], true); $totals['errors'] += $results['total_results']['errors']; @@ -123,11 +133,12 @@ protected static function combineJobResults($job_group) { $totals['suggestions'] += $results['total_results']['suggestions']; - if (!empty($results['scan_results']['module_urls'])){ + if (!empty($results['scan_results']['module_urls'])) { $module_urls = array_merge($module_urls, $results['scan_results']['module_urls']); } - if (!empty($results['scan_results']['unscannable'])){ - $unscannables = array_merge($unscannables , $results['scan_results']['unscannable']); + + if (!empty($results['scan_results']['unscannable'])) { + $unscannables = array_merge($unscannables, $results['scan_results']['unscannable']); } unset($results['module_urls']); @@ -157,23 +168,30 @@ protected static function combineJobResults($job_group) { ]; } - protected static function getNextJob() { + protected static function getNextJob() + { // it's important that this claims the next job, not allowing other processes to grab it. // we LOCK this row using 'FOR UPDATE' then update it's status so other queries don't find it UdoitDB::beginTransaction(); $sql = "SELECT * FROM job_queue WHERE status = 'new'"; - if(UdoitDB::$type !== 'test') $sql .= " FOR UPDATE"; // SQLITE doesn't support SELECT... FOR UPDATE - if( ! ($query = UdoitDB::query($sql))) return false; // return false if theres nothing + if ('test' !== UdoitDB::$type) { + $sql .= " FOR UPDATE"; // SQLITE doesn't support SELECT... FOR UPDATE + } + if (!($query = UdoitDB::query($sql))) { + return false; // return false if theres nothing + } $job = $query->fetchObject(); $sql = "UPDATE job_queue SET status = 'running' WHERE id = '{$job->id}' AND status = 'new'"; UdoitDB::query($sql); UdoitDB::commit(); + return $job; } - protected static function finishJobWithResults($job_id, $result) { - $sql ="UPDATE job_queue SET status = :status, results = :results, date_completed = CURRENT_TIMESTAMP WHERE id = :job_id"; + protected static function finishJobWithResults($job_id, $result) + { + $sql = "UPDATE job_queue SET status = :status, results = :results, date_completed = CURRENT_TIMESTAMP WHERE id = :job_id"; $sth = UdoitDB::prepare($sql); $sth->bindValue(':job_id', $job_id); $sth->bindValue(':status', 'finished'); @@ -181,7 +199,8 @@ protected static function finishJobWithResults($job_id, $result) { $sth->execute(); } - protected static function updateJobStatus($job, $status) { + protected static function updateJobStatus($job, $status) + { $sql = "UPDATE job_queue SET status = '{$status}' WHERE id = {$job->id}"; UdoitDB::query($sql); } diff --git a/lib/UdoitUtils.php b/lib/UdoitUtils.php index 47158fc88..ba2a40f43 100644 --- a/lib/UdoitUtils.php +++ b/lib/UdoitUtils.php @@ -38,12 +38,17 @@ class UdoitUtils private static $instance; - public static function instance(){ - if(!isset(self::$instance)) self::$instance = new UdoitUtils(); + public static function instance() + { + if (!isset(self::$instance)) { + self::$instance = new UdoitUtils(); + } + return self::$instance; } - public static function setupOauth($id, $key, $uri, $consumer_key, $secret){ + public static function setupOauth($id, $key, $uri, $consumer_key, $secret) + { self::$canvas_oauth_id = $id; self::$canvas_oauth_key = $key; self::$canvas_oauth_uri = $uri; @@ -54,75 +59,81 @@ public static function setupOauth($id, $key, $uri, $consumer_key, $secret){ public function getYouTubeId($link_url) { $matches = null; - foreach(self::$regex as $pattern) { + foreach (self::$regex as $pattern) { if (preg_match($pattern, $link_url, $matches)) { return $matches[1]; } } + return null; } - public function exitWithPageError($error) { + public function exitWithPageError($error) + { $templates = new League\Plates\Engine(__DIR__.'/../templates'); echo($templates->render('error', ['error' => $error])); error_log($error); exit(); } - public function exitWithPartialError($error) { + public function exitWithPartialError($error) + { $templates = new League\Plates\Engine(__DIR__.'/../templates'); echo($templates->render('partials/error', ['error' => $error])); error_log($error); exit(); } - protected function curlOauthToken($base_url, $post_data) { - // @TODO - why not use Httpful here? - $ch = curl_init("{$base_url}/login/oauth2/token"); - curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data)); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - $result = curl_exec($ch); - curl_close($ch); - - return json_decode($result); - } - - public function getLocalApiKey($user_id){ + public function getLocalApiKey($user_id) + { global $db_user_table; $sth = UdoitDB::prepare("SELECT api_key, canvas_url FROM {$db_user_table} WHERE id = :userid LIMIT 1"); $sth->bindValue(':userid', $user_id, PDO::PARAM_INT); $sth->execute(); - if ($result = $sth->fetchObject()){ + if ($result = $sth->fetchObject()) { self::$canvas_base_url = $result->canvas_url; + return $result->api_key; } + return false; } - public function getValidRefreshedApiKey($user_id){ + public function getValidRefreshedApiKey($user_id) + { // check our db for an existing api key $api_key = UdoitUtils::instance()->getLocalApiKey($user_id); - if( ! empty($api_key)){ - if(UdoitUtils::instance()->validateApiKey($user_id, $api_key)) return $api_key; - if($api_key = UdoitUtils::instance()->refreshApiKey($user_id)) return $api_key; + if (!empty($api_key)) { + if (UdoitUtils::instance()->validateApiKey($user_id, $api_key)) { + return $api_key; + } + if ($api_key = UdoitUtils::instance()->refreshApiKey($user_id)) { + return $api_key; + } } + return false; } - public function validateApiKey($user_id, $api_key) { + public function validateApiKey($user_id, $api_key) + { global $UDOIT_ENV; - if(empty($user_id) || empty($api_key)) return false; + if (empty($user_id) || empty($api_key)) { + return false; + } // return false for testing - if ($UDOIT_ENV !== ENV_PROD) return false; + if (ENV_PROD !== $UDOIT_ENV) { + return false; + } // @TODO use self $url = self::$canvas_base_url."api/v1/users/{$user_id}/profile"; $resp = Httpful\Request::get($url) ->addHeader('Authorization', "Bearer {$api_key}") ->send(); + return isset($resp->body->id); } @@ -131,59 +142,70 @@ public function getRefreshToken($user_id) { global $db_user_table; - if ($UDOIT_ENV !== ENV_PROD) return "test-token"; // return false for testing + if (ENV_PROD !== $UDOIT_ENV) { + return "test-token"; // return false for testing + } $sth = UdoitDB::prepare("SELECT refresh_token FROM {$db_user_table} WHERE id = :userid LIMIT 1"); $sth->bindValue(':userid', $user_id, PDO::PARAM_INT); $sth->execute(); - if ($result = $sth->fetchObject()) return $result->refresh_token; + if ($result = $sth->fetchObject()) { + return $result->refresh_token; + } + return false; } - public function refreshApiKey($user_id) { + public function refreshApiKey($user_id) + { global $UDOIT_ENV; global $db_user_table; $refresh_token = $this->getRefreshToken($user_id); - if( ! $refresh_token) return false; + if (!$refresh_token) { + return false; + } $post_data = [ 'grant_type' => 'refresh_token', 'client_id' => self::$canvas_oauth_id, 'redirect_uri' => self::$canvas_oauth_uri, 'client_secret' => self::$canvas_oauth_key, - 'refresh_token' => $refresh_token + 'refresh_token' => $refresh_token, ]; $json_result = $this->curlOauthToken(self::$canvas_base_url, $post_data); // update the token in the database - if(isset($json_result->access_token) && $this->validateApiKey($user_id, $json_result->access_token)){ + if (isset($json_result->access_token) && $this->validateApiKey($user_id, $json_result->access_token)) { $sth = UdoitDB::prepare("UPDATE {$db_user_table} set api_key = :api_key, refresh_token = :refresh_token WHERE id = :userid LIMIT 1"); $sth->bindValue(':userid', $userID, PDO::PARAM_INT); $sth->bindValue(':api_key', $json_result->access_token, PDO::PARAM_STR); $sth->bindValue(':refresh_token', $json_result->refresh_token, PDO::PARAM_STR); $sth->execute(); + return $json_result->access_token; } return false; } - public function authorizeNewApiKey($base_url, $code) { + public function authorizeNewApiKey($base_url, $code) + { $post_data = [ 'grant_type' => 'authorization_code', 'client_id' => self::$canvas_oauth_id, 'redirect_uri' => self::$canvas_oauth_uri, 'client_secret' => self::$canvas_oauth_key, - 'code' => $code + 'code' => $code, ]; return $this->curlOauthToken($base_url, $post_data); } - public function createOrUpdateUser($user_id, $api_key, $refresh_token, $canvas_url){ + public function createOrUpdateUser($user_id, $api_key, $refresh_token, $canvas_url) + { global $db_user_table; $sth = UdoitDB::prepare("SELECT * FROM {$db_user_table} WHERE id = :userid AND canvas_url = :canvas_url LIMIT 1"); @@ -205,9 +227,24 @@ public function createOrUpdateUser($user_id, $api_key, $refresh_token, $canvas_u return $sth->execute(); } - public function verifyBasicLTILaunch(){ + public function verifyBasicLTILaunch() + { require_once(__DIR__.'/ims-blti/blti.php'); $context = new BLTI(self::$canvas_consumer_key, self::$canvas_secret_key, false, false); + return isset($context->valid) && $context->valid; } + + protected function curlOauthToken($base_url, $post_data) + { + // @TODO - why not use Httpful here? + $ch = curl_init("{$base_url}/login/oauth2/token"); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data)); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $result = curl_exec($ch); + curl_close($ch); + + return json_decode($result); + } } diff --git a/lib/Ufixit.php b/lib/Ufixit.php index 2ee3ad98a..ae4f44e21 100755 --- a/lib/Ufixit.php +++ b/lib/Ufixit.php @@ -17,7 +17,6 @@ * * Primary Author Contact: Jacob Bates */ -require_once(__DIR__.'/quail/quail/quail.php'); use Httpful\Request; use zz\Html\HTMLMinify; @@ -80,7 +79,7 @@ class Ufixit /** * How many times str_replace should do the job - * @var integer + * @var int */ public $replacement_count = 1; @@ -102,11 +101,12 @@ public function __construct($data) * @param string $error_html - The bad html that needs to be fixed * @param string $new_content - The new content from the user * @param bool $submitting_again - If the user is resubmitting their error fix + * * @return string $fixed_img - The image with new alt text */ public function fixAltText($error_html, $new_content, $submitting_again = false) { - $this->dom->loadHTML('' . $error_html); + $this->dom->loadHTML("{$error_html}"); $imgs = $this->dom->getElementsByTagName('img'); $fixed_img = null; @@ -129,6 +129,7 @@ public function fixAltText($error_html, $new_content, $submitting_again = false) * @param bool $bold - Boolean whether resulting text should be stylized bold * @param bool $italic - Boolean whether resulting text should be stylized italicised * @param bool $submitting_again - If the user is resubmitting their error fix + * * @return string $fixed_css - The html with corrected CSS */ public function fixCssColor($error_html, $new_content, $bold, $italic, $submitting_again = false) @@ -136,18 +137,18 @@ public function fixCssColor($error_html, $new_content, $bold, $italic, $submitti preg_match_all('/<(\w+)\s+\w+.*?>/s', $error_html, $matches); $fixed_css = $error_html; - + $fixed_css = preg_replace('/background:\s*([#a-z0-9]*)\s*;*\s*/', '', $fixed_css); $fixed_css = preg_replace('/background-color:\s*([#a-z0-9]*)\s*;*\s*/', '', $fixed_css); $fixed_css = preg_replace('/color:\s*([#a-z0-9]*)\s*;*\s*/', '', $fixed_css); $fixed_css = preg_replace('/font-weight:\s*([a-z0-9]*)\s*;*\s*/', '', $fixed_css); $fixed_css = preg_replace('/font-style:\s*([a-z0-9]*)\s*;*\s*/', '', $fixed_css); - $fixed_css = preg_replace('/style="/', 'style="background-color: ' . $new_content[0] . '; color: ' . $new_content[1] . ';', $fixed_css); + $fixed_css = preg_replace('/style="/', 'style="background-color: '.$new_content[0].'; color: '.$new_content[1].';', $fixed_css); + + $this->dom->loadHTML(''.$fixed_css); - $this->dom->loadHTML('' . $fixed_css ); + $tag = $this->dom->getElementsByTagName($matches[1][0])->item(0); - $tag = $this->dom->getElementsByTagName( $matches[1][0] )->item(0); - if ($bold) { $tag->setAttribute('style', $tag->getAttribute('style').' font-weight: bold;'); } else { @@ -161,6 +162,7 @@ public function fixCssColor($error_html, $new_content, $bold, $italic, $submitti } $fixed_css = $this->dom->saveHTML($tag); + return $fixed_css; } @@ -171,6 +173,7 @@ public function fixCssColor($error_html, $new_content, $bold, $italic, $submitti * @param bool $bold - Boolean whether resulting text should be stylized bold * @param bool $italic - Boolean whether resulting text should be stylized italicised * @param bool $submitting_again - If the user is resubmitting their error fix + * * @return string $fixed_css - The html with corrected CSS */ public function fixCssEmphasize($error_html, $bold, $italic, $remove_color, $submitting_again = false) @@ -179,15 +182,15 @@ public function fixCssEmphasize($error_html, $bold, $italic, $remove_color, $sub $fixed_css = $error_html; - if($remove_color) { + if ($remove_color) { $fixed_css = preg_replace('/background:\s*([#a-z0-9]*)\s*;*\s*/', '', $fixed_css); $fixed_css = preg_replace('/background-color:\s*([#a-z0-9]*)\s*;*\s*/', '', $fixed_css); $fixed_css = preg_replace('/color:\s*([#a-z0-9]*)\s*;*\s*/', '', $fixed_css); } - $this->dom->loadHTML('' . $fixed_css ); + $this->dom->loadHTML(''.$fixed_css); - $tag = $this->dom->getElementsByTagName( $matches[1][0] )->item(0); + $tag = $this->dom->getElementsByTagName($matches[1][0])->item(0); if ($bold) { $tag->setAttribute('style', $tag->getAttribute('style').' font-weight: bold;'); @@ -211,20 +214,21 @@ public function fixCssEmphasize($error_html, $bold, $italic, $remove_color, $sub * @param string $error_html - The bad html that needs to be fixed * @param string|array $new_content - The new Heading text from the user * @param bool $submitting_again - If the user is resubmitting their error fix + * * @return string $fixed_css - The html with corrected Link */ public function fixLink($error_html, $new_content, $submitting_again = false) { $fixed_link = ''; - if ($new_content == '') { + if ('' === $new_content) { return $fixed_link; } - $this->dom->loadHTML('' . $error_html); + $this->dom->loadHTML(''.$error_html); $tag = $this->dom->getElementsByTagName('a')->item(0); - $linkText = $this->dom->createTextNode( htmlspecialchars($new_content) ); + $linkText = $this->dom->createTextNode(htmlspecialchars($new_content)); $tag->nodeValue = ""; $tag->appendChild($linkText); @@ -239,23 +243,24 @@ public function fixLink($error_html, $new_content, $submitting_again = false) * @param string $error_html - The bad html that needs to be fixed * @param string|array $new_content - The new Heading text from the user * @param bool $submitting_again - If the user is resubmitting their error fix + * * @return string $fixed_heading - The html with corrected Heading */ public function fixHeading($error_html, $new_content, $submitting_again = false) { $fixed_heading = ''; - if ($new_content == '') { + if ('' === $new_content) { return $fixed_heading; } - $matches = array(); + $matches = []; preg_match('/h[0-6]/i', $error_html, $matches); - $this->dom->loadHTML('' . $error_html); + $this->dom->loadHTML(''.$error_html); - $tag = $this->dom->getElementsByTagName( $matches[0] )->item(0); + $tag = $this->dom->getElementsByTagName($matches[0])->item(0); - $headingText = $this->dom->createTextNode( htmlspecialchars($new_content) ); + $headingText = $this->dom->createTextNode(htmlspecialchars($new_content)); $tag->appendChild($headingText); @@ -269,17 +274,17 @@ public function fixHeading($error_html, $new_content, $submitting_again = false) * @param string $error_html - The unmodified table without proper headings * @param string $selected_header - The selected table row or column * @param boolean $submitting_again - If the user is resubmitting their error fix + * * @return array $new_data - Array of the corrected error and the original table row/column */ public function fixTableHeaders($error_html, $selected_header, $submitting_again = false) { - $new_data = [ 'old' => '', - 'fixed' => '' + 'fixed' => '', ]; - $this->dom->loadHTML('' . $error_html); + $this->dom->loadHTML(''.$error_html); switch ($selected_header) { case 'col': @@ -297,8 +302,8 @@ public function fixTableHeaders($error_html, $selected_header, $submitting_again $new_data['fixed'] .= $this->dom->saveHTML($tr); } - break; + case 'row': $tr = $this->dom->getElementsByTagName('tr')->item(0); $new_data['old'] = $this->dom->saveHTML($tr); @@ -314,8 +319,8 @@ public function fixTableHeaders($error_html, $selected_header, $submitting_again } $new_data['fixed'] = $this->dom->saveHTML($tr); - break; + case 'both': $first = true; $trs = $this->dom->getElementsByTagName('tr'); @@ -361,11 +366,12 @@ public function fixTableHeaders($error_html, $selected_header, $submitting_again * @param string $error_html - The color(s) that need to be replaced * @param string $new_content - The new CSS color(s) from the user * @param bool $submitting_again - If the user is resubmitting their error fix + * * @return string $fixed_ths - The html with corrected th scopes */ public function fixTableThScopes($error_html, $new_content, $submitting_again = false) { - $this->dom->loadHTML('' . $error_html); + $this->dom->loadHTML(''.$error_html); $ths = $this->dom->getElementsByTagName('th'); @@ -389,7 +395,7 @@ public function getFile($start_id) $folder = Request::get($folder_uri)->send(); $curled_file = []; - if ($start_id == "root") { + if ("root" === $start_id) { $start_id = $folder->body->id; } @@ -411,7 +417,7 @@ public function getFile($start_id) foreach ($files->body as $file) { $mac_check = (substr($file->display_name, 0, 2)); // Don't capture mac files, ._ - if ($mac_check !== "._" and $file->id == $this->content_id) { + if ("._" !== $mac_check and $this->content_id == $file->id) { $curled_file['id'] = $file->id; $curled_file['name'] = $file->display_name; $curled_file['parent_folder_path'] = $folder_name; @@ -423,17 +429,14 @@ public function getFile($start_id) $page_num++; } - } //if there are files in the current folder if ($num_folders > 0) { $subfolders = Request::get($this->base_uri."/api/v1/folders/".$start_id."/folders?access_token=".$this->api_key)->send(); foreach ($subfolders->body as $subfolder) { - $foo = $this->getFile($subfolder->id); - - if ($foo !== false) { - return $foo; + if ($file = $this->getFile($subfolder->id)) { + return $file; } } } //if there are subfolders @@ -445,6 +448,7 @@ public function getFile($start_id) * Renames an element and preserves its attributes * @param object $node - The DOMElement object you wish to rename * @param string $name - The new name for the element + * * @return object - The renamed DOMElement */ public function renameElement($node, $name) @@ -466,8 +470,9 @@ public function renameElement($node, $name) if (!is_null($attribute->namespaceURI)) { $renamed->setAttributeNS('http://www.w3.org/2000/xmlns/', - 'xmlns:'.$attribute->prefix, - $attribute->namespaceURI); + 'xmlns:'.$attribute->prefix, + $attribute->namespaceURI + ); } $renamed->setAttributeNode($attribute); @@ -483,6 +488,7 @@ public function renameElement($node, $name) * @param string $html - html from page being edited * @param string $error - original html of error being fixed * @param string $corrected - resulting html or error after fix + * * @return string $html - html after corrected html has replaced error html */ public function replaceContent($html, $error, $corrected) @@ -533,6 +539,7 @@ public function uploadFixedDiscussions($corrected_error, $error_html) * [uploadFixedFiles description] * @param [type] $corrected_error [description] * @param [type] $error_html [description] + * * @return [type] [description] */ public function uploadFixedFiles($corrected_error, $error_html) @@ -565,7 +572,7 @@ public function uploadFixedFiles($corrected_error, $error_html) 'size' => filesize($file), 'content_type' => "text/html", 'parent_folder_path' => $this->curled_file['parent_folder_path'], - 'on_duplicate' => "overwrite" + 'on_duplicate' => "overwrite", ]; $uri = $this->base_uri."/api/v1/courses/".$this->course_id."/files?&access_token=".$this->api_key; @@ -576,7 +583,7 @@ public function uploadFixedFiles($corrected_error, $error_html) $upload_uri = $response->upload_url; //send everything again - $post_data = (array)$response->upload_params; + $post_data = (array) $response->upload_params; $f = fopen($file, "r"); $post_data['file'] = fread($f, filesize($file)); @@ -587,7 +594,7 @@ public function uploadFixedFiles($corrected_error, $error_html) preg_match('/Location\: (.*)\\n/', $response->raw_headers, $matches); //error - if ( ! isset($matches[1])) { + if (!isset($matches[1])) { error_log(print_r($response, true)); } @@ -619,8 +626,7 @@ public function uploadFixedPages($corrected_error, $error_html) // get the current page content $get_uri = "{$this->base_uri}/api/v1/courses/{$this->course_id}/pages/{$this->content_id}?access_token={$this->api_key}"; $page_resp = Request::get($get_uri)->send(); - if($page_resp->hasErrors() || ! $page_resp->hasBody()) - { + if ($page_resp->hasErrors() || !$page_resp->hasBody()) { throw new \Exception("Error getting page to update: course: {$this->course_id} page: {$this->content_id}"); } @@ -630,8 +636,7 @@ public function uploadFixedPages($corrected_error, $error_html) $put_uri = "{$this->base_uri}/api/v1/courses/{$this->course_id}/pages/{$this->content_id}?&access_token={$this->api_key}"; $put_resp = Request::put($put_uri)->body(['wiki_page[body]' => $html])->sendsType(\Httpful\Mime::FORM)->send(); - if($put_resp->hasErrors() || ! $put_resp->hasBody()) - { + if ($put_resp->hasErrors() || !$put_resp->hasBody()) { throw new \Exception("Error updating: course: {$this->course_id} page: {$this->content_id}"); } } diff --git a/lib/worker.php b/lib/worker.php index 94bf129f4..40b738277 100644 --- a/lib/worker.php +++ b/lib/worker.php @@ -19,24 +19,21 @@ */ require_once(__DIR__.'/../config/settings.php'); -if (php_sapi_name() !== "cli"){ +if (php_sapi_name() !== "cli") { $logger->addError('This worker can only be run on the command line.'); echo('This worker can only be run on the command line.'); } -while(true) -{ +while (true) { // run the next job UdoitJob::runNextJob(); // take a nap if there's no more work to do - if(UdoitJob::countJobsRemaining() < 1) - { + if (UdoitJob::countJobsRemaining() < 1) { $logger->addInfo('Worker Sleeping'); UdoitDB::disconnect(); // allow the db to disconnect sleep($worker_sleep_seconds); - } - else{ + } else { sleep(1); } } diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 000000000..0090d0221 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,129 @@ + + + The UCFCDL coding standard based. + + + + + + + + + + + + */vendor/* + */lib/quail/* + */ims-blti/* + */templates/* + public/udoit.xml.php + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 0 + + + + + 0 + + + + warning + + + + + 0 + + + 0 + + + 0 + + + + 0 + + + + warning + + + + error + + + + error + + + + 0 + + + + There should always be a description, followed by a blank line, before the tags of a class comment. + + + + + diff --git a/public/assets/js/default.js b/public/assets/js/default.js index 7c57b8293..023fd4afb 100755 --- a/public/assets/js/default.js +++ b/public/assets/js/default.js @@ -33,7 +33,7 @@ function killButton(callback) { }); } - $('#udoitForm button.submit').removeClass('disabled').html('Scan This Course'); + $('#udoitForm button.submit').removeClass('disabled').html('Scan This Course Again'); $('#waitMsg').fadeOut(); diff --git a/public/cached.php b/public/cached.php index 19f4471d5..63d54f665 100755 --- a/public/cached.php +++ b/public/cached.php @@ -1,21 +1,21 @@ . +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . * -* Primary Author Contact: Jacob Bates +* Primary Author Contact: Jacob Bates */ require_once('../config/settings.php'); @@ -27,22 +27,22 @@ session_write_close(); if (!$sth->execute()) { - UdoitUtils::instance()->exitWithError('Could not complete database query'); + UdoitUtils::instance()->exitWithError('Could not complete database query'); } $reports = $sth->fetchAll(); // Add the test report if not in production mode -if ($UDOIT_ENV != ENV_PROD) { - $reports[] = [ - 'id' => 'TEST', - 'user_id' => 0, - 'course_id' => 'TEST', - 'file_path' => 'reports/test.json', - 'date_run' => 'TEST: 1998 (when section 508 was ammended)', - 'errors' => 64, - 'suggestions' => 32 - ]; +if (ENV_PROD !== $UDOIT_ENV) { + $reports[] = [ + 'id' => 'TEST', + 'user_id' => 0, + 'course_id' => 'TEST', + 'file_path' => 'reports/test.json', + 'date_run' => 'TEST: 1998 (when section 508 was ammended)', + 'errors' => 64, + 'suggestions' => 32, + ]; } $templates = new League\Plates\Engine(__DIR__.'/../templates'); diff --git a/public/index.php b/public/index.php index b910d233c..a30776925 100755 --- a/public/index.php +++ b/public/index.php @@ -1,21 +1,21 @@ . +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . * -* Primary Author Contact: Jacob Bates +* Primary Author Contact: Jacob Bates */ require_once(__DIR__.'/../config/settings.php'); @@ -27,16 +27,16 @@ print_r($post_input); // verify we have the variables we need from the LTI launch -$expect = ['oauth_consumer_key','custom_canvas_api_domain','custom_canvas_user_id','custom_canvas_course_id']; +$expect = ['oauth_consumer_key', 'custom_canvas_api_domain', 'custom_canvas_user_id', 'custom_canvas_course_id']; foreach ($expect as $key) { - if(empty($post_input[$key])){ - UdoitUtils::instance()->exitWithPageError("Missing LTI launch information. Please ensure that your instance of UDOIT is installed to Canvas correctly. Missing: {$key}"); - } + if (empty($post_input[$key])) { + UdoitUtils::instance()->exitWithPageError("Missing LTI launch information. Please ensure that your instance of UDOIT is installed to Canvas correctly. Missing: {$key}"); + } } // verify LTI launch -if ( ! UdoitUtils::instance()->verifyBasicLTILaunch()) { - UdoitUtils::instance()->exitWithPageError('LTI/Oauth verification problem, please ensure that your instance of UDOIT is configured correctly.'); +if (!UdoitUtils::instance()->verifyBasicLTILaunch()) { + UdoitUtils::instance()->exitWithPageError('LTI/Oauth verification problem, please ensure that your instance of UDOIT is configured correctly.'); } // store LTI launch variables @@ -45,10 +45,10 @@ UdoitUtils::$canvas_base_url = "https://{$post_input['custom_canvas_api_domain']}/"; $_SESSION['base_url'] = UdoitUtils::$canvas_base_url; $_SESSION['launch_params'] = [ - 'custom_canvas_user_id' => $post_input['custom_canvas_user_id'], - 'custom_canvas_course_id' => $post_input['custom_canvas_course_id'], - 'context_label' => $post_input['context_label'], - 'context_title' => $post_input['context_title'], + 'custom_canvas_user_id' => $post_input['custom_canvas_user_id'], + 'custom_canvas_course_id' => $post_input['custom_canvas_course_id'], + 'context_label' => $post_input['context_label'], + 'context_title' => $post_input['context_title'], ]; $api_key = UdoitUtils::instance()->getValidRefreshedApiKey($user_id); diff --git a/public/oauth2response.php b/public/oauth2response.php index 3eedc307e..679ba5e9f 100755 --- a/public/oauth2response.php +++ b/public/oauth2response.php @@ -1,45 +1,45 @@ -. -* -* Primary Author Contact: Jacob Bates -*/ -require_once('../config/settings.php'); - -$get_input = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING); // Sanitize $_GET global - -if (isset($get_input['error'])) { - UdoitUtils::instance()->exitWithPageError('Authentication problem: Access Denied.'); -} - -if ( ! isset($get_input['code'])) { - UdoitUtils::instance()->exitWithPageError('Authentication problem, please ensure that your instance of UDOIT is configured correctly.'); -} - -session_start(); -// get session variables -$canvas_user_id = $_SESSION['launch_params']['custom_canvas_user_id']; -$canvas_uri = $_SESSION['base_url']; -$new_key = UdoitUtils::instance()->authorizeNewApiKey($canvas_uri, $get_input['code']); - -// It should have access_token and refresh_token -if ( ! isset($new_key->access_token) || ! isset($new_key->refresh_token)) { - UdoitUtils::instance()->exitWithPageError('Authentication problem: Please contact support.'); -} - -UdoitUtils::instance()->createOrUpdateUser($canvas_user_id, $new_key->access_token, $new_key->refresh_token, $canvas_uri); - -header('Location: scanner.php'); +. +* +* Primary Author Contact: Jacob Bates +*/ +require_once('../config/settings.php'); + +$get_input = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING); // Sanitize $_GET global + +if (isset($get_input['error'])) { + UdoitUtils::instance()->exitWithPageError('Authentication problem: Access Denied.'); +} + +if (!isset($get_input['code'])) { + UdoitUtils::instance()->exitWithPageError('Authentication problem, please ensure that your instance of UDOIT is configured correctly.'); +} + +session_start(); +// get session variables +$canvas_user_id = $_SESSION['launch_params']['custom_canvas_user_id']; +$canvas_uri = $_SESSION['base_url']; +$new_key = UdoitUtils::instance()->authorizeNewApiKey($canvas_uri, $get_input['code']); + +// It should have access_token and refresh_token +if (!isset($new_key->access_token) || !isset($new_key->refresh_token)) { + UdoitUtils::instance()->exitWithPageError('Authentication problem: Please contact support.'); +} + +UdoitUtils::instance()->createOrUpdateUser($canvas_user_id, $new_key->access_token, $new_key->refresh_token, $canvas_uri); + +header('Location: scanner.php'); diff --git a/public/parsePdf.php b/public/parsePdf.php index 24b635ddb..607401c3e 100755 --- a/public/parsePdf.php +++ b/public/parsePdf.php @@ -1,21 +1,21 @@ . +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . * -* Primary Author Contact: Jacob Bates +* Primary Author Contact: Jacob Bates */ require_once('../config/settings.php'); diff --git a/public/parsePdfProgress.php b/public/parsePdfProgress.php index c18b39528..e7a2814a3 100755 --- a/public/parsePdfProgress.php +++ b/public/parsePdfProgress.php @@ -1,21 +1,21 @@ . +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . * -* Primary Author Contact: Jacob Bates +* Primary Author Contact: Jacob Bates */ session_start(); UdoitUtils::$canvas_base_url = $_SESSION['base_url']; diff --git a/public/parseResults.php b/public/parseResults.php index aa866645e..798e726d8 100755 --- a/public/parseResults.php +++ b/public/parseResults.php @@ -1,44 +1,46 @@ . +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . * -* Primary Author Contact: Jacob Bates +* Primary Author Contact: Jacob Bates */ require_once('../config/settings.php'); $get_input = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING); -if ( ! isset($get_input['path'])) $get_input['path'] = ''; +if (!isset($get_input['path'])) { + $get_input['path'] = ''; +} // Load the test report in test/dev mode and no other report is selected -if ($UDOIT_ENV !== ENV_PROD && !isset($get_input['report_id'])) { - $get_input['report_id'] = 'TEST'; // TEST is the id of the test report +if (ENV_PROD !== $UDOIT_ENV && !isset($get_input['report_id'])) { + $get_input['report_id'] = 'TEST'; // TEST is the id of the test report } // @TODO: make sure this user's got a session -if ( ! isset($get_input['report_id'])) { - UdoitUtils::instance()->exitWithPartialError('No report requested'); +if (!isset($get_input['report_id'])) { + UdoitUtils::instance()->exitWithPartialError('No report requested'); } $sth = UdoitDB::prepare("SELECT * FROM {$db_reports_table} WHERE id = :report_id"); $sth->bindValue(':report_id', $get_input['report_id'], PDO::PARAM_INT); -if ( ! $sth->execute()) { - error_log(print_r($sth->errorInfo(), true)); - UdoitUtils::instance()->exitWithPartialError('Error searching for report'); +if (!$sth->execute()) { + error_log(print_r($sth->errorInfo(), true)); + UdoitUtils::instance()->exitWithPartialError('Error searching for report'); } $report_json = $sth->fetch(PDO::FETCH_OBJ)->report_json; @@ -46,18 +48,18 @@ $report = json_decode($report_json); if (is_null($report)) { - $json_error = json_last_error_msg(); - UdoitUtils::instance()->exitWithPartialError("Cannot parse this report. JSON error $json_error."); + $json_error = json_last_error_msg(); + UdoitUtils::instance()->exitWithPartialError("Cannot parse this report. JSON error {$json_error}."); } $results = [ - 'course' => $report->course, - 'error_count' => $report->total_results->errors, - 'suggestion_count' => $report->total_results->suggestions, - 'report_groups' => $report->content, - 'post_path' => $get_input['path'], - 'fixable_error_types' => ["cssTextHasContrast", "imgNonDecorativeHasAlt", "tableDataShouldHaveTh", "tableThShouldHaveScope", "headersHaveText", "aMustContainText", "imgAltIsDifferent", "imgAltIsTooLong"], - 'fixable_suggestions' => ["aSuspiciousLinkText", "imgHasAlt", "aLinkTextDoesNotBeginWithRedundantWord", "cssTextStyleEmphasize"] + 'course' => $report->course, + 'error_count' => $report->total_results->errors, + 'suggestion_count' => $report->total_results->suggestions, + 'report_groups' => $report->content, + 'post_path' => $get_input['path'], + 'fixable_error_types' => ["cssTextHasContrast", "imgNonDecorativeHasAlt", "tableDataShouldHaveTh", "tableThShouldHaveScope", "headersHaveText", "aMustContainText", "imgAltIsDifferent", "imgAltIsTooLong"], + 'fixable_suggestions' => ["aSuspiciousLinkText", "imgHasAlt", "aLinkTextDoesNotBeginWithRedundantWord", "cssTextStyleEmphasize"], ]; diff --git a/public/process.php b/public/process.php index c82f1a62e..6c1e80480 100755 --- a/public/process.php +++ b/public/process.php @@ -29,7 +29,7 @@ $main_action = filter_input(INPUT_POST, 'main_action', FILTER_SANITIZE_STRING); switch ($main_action) { case 'udoit': - if ($UDOIT_ENV != ENV_PROD) { + if (ENV_PROD !== $UDOIT_ENV) { require 'parseResults.php'; exit(); } @@ -40,7 +40,7 @@ $job_group = uniqid('job_', true); // uniqid for this group of jobs // No content selected - if ($content == 'none') { + if ('none' === $content) { UdoitUtils::instance()->exitWithPartialError('Please select which course content you wish to scan above.'); } @@ -50,7 +50,7 @@ 'base_uri' => $base_url, 'title' => $title, 'course_id' => $course_id, - 'scan_item' => $scan_item + 'scan_item' => $scan_item, ]; // create an id to group all these jobs together @@ -69,7 +69,6 @@ break; case 'ufixit': - $data = [ 'base_uri' => $base_url, 'content_id' => filter_input(INPUT_POST, 'contentid', FILTER_SANITIZE_STRING), @@ -80,12 +79,12 @@ 'italic' => (filter_input(INPUT_POST, 'add-italic', FILTER_SANITIZE_STRING) == 'italic'), 'remove_color' => (filter_input(INPUT_POST, 'remove-color', FILTER_SANITIZE_STRING) == 'true'), 'course_id' => filter_input(INPUT_POST, 'course_id', FILTER_SANITIZE_NUMBER_INT), - 'api_key' => UdoitUtils::instance()->getLocalApiKey($user_id) + 'api_key' => UdoitUtils::instance()->getLocalApiKey($user_id), ]; $ufixit = new Ufixit($data); - if (strtolower($data['content_type']) == 'files') { + if (strtolower($data['content_type']) === 'files') { $ufixit->curled_file = $ufixit->getFile("root"); if ($ufixit->curled_file == false) { diff --git a/public/progress.php b/public/progress.php index 79b8b28f2..4c0f8abc2 100755 --- a/public/progress.php +++ b/public/progress.php @@ -1,61 +1,60 @@ -. -* -* Primary Author Contact: Jacob Bates -*/ - -require_once('../config/settings.php'); - -session_start(); -$user_id = $_SESSION['launch_params']['custom_canvas_user_id']; -UdoitUtils::$canvas_base_url = $_SESSION['base_url']; -session_write_close(); - -$job_group = filter_input(INPUT_GET, 'job_group', FILTER_SANITIZE_STRING); - -// get the non-finalize jobs -$sth = UdoitDB::prepare("SELECT id, data, job_type, status FROM job_queue WHERE job_type != 'finalize_report' AND job_group = :job_group AND user_id = :user_id"); -$sth->bindValue(":job_group", $job_group); -$sth->bindValue(":user_id", $user_id); -$sth->execute(); -$jobs = $sth->fetchAll(); - -// get the finalize job (this is done in 2 queries so we don't have to retrieve the results from the non-final jobs) -$sth = UdoitDB::prepare("SELECT * FROM job_queue WHERE job_type = 'finalize_report' AND job_group = :job_group AND user_id = :user_id"); -$sth->bindValue(":job_group", $job_group); -$sth->bindValue(":user_id", $user_id); -$sth->execute(); -$final_job = $sth->fetch(); -$final_job['results'] = json_decode($final_job['results'], true); - -$job_status = []; -foreach ($jobs as $job) -{ - $json = json_decode($job['data'], true); - $job_status[] = [ - 'type' => $json['scan_item'], - 'status' => $job['status'] - ]; -} - -$result = [ - 'reportID' => isset($final_job['results']['report_id']) ? $final_job['results']['report_id'] : null, - 'status' => $final_job['status'], - 'jobs' => $job_status -]; - -echo(json_encode($result)); +. +* +* Primary Author Contact: Jacob Bates +*/ + +require_once('../config/settings.php'); + +session_start(); +$user_id = $_SESSION['launch_params']['custom_canvas_user_id']; +UdoitUtils::$canvas_base_url = $_SESSION['base_url']; +session_write_close(); + +$job_group = filter_input(INPUT_GET, 'job_group', FILTER_SANITIZE_STRING); + +// get the non-finalize jobs +$sth = UdoitDB::prepare("SELECT id, data, job_type, status FROM job_queue WHERE job_type != 'finalize_report' AND job_group = :job_group AND user_id = :user_id"); +$sth->bindValue(":job_group", $job_group); +$sth->bindValue(":user_id", $user_id); +$sth->execute(); +$jobs = $sth->fetchAll(); + +// get the finalize job (this is done in 2 queries so we don't have to retrieve the results from the non-final jobs) +$sth = UdoitDB::prepare("SELECT * FROM job_queue WHERE job_type = 'finalize_report' AND job_group = :job_group AND user_id = :user_id"); +$sth->bindValue(":job_group", $job_group); +$sth->bindValue(":user_id", $user_id); +$sth->execute(); +$final_job = $sth->fetch(); +$final_job['results'] = json_decode($final_job['results'], true); + +$job_status = []; +foreach ($jobs as $job) { + $json = json_decode($job['data'], true); + $job_status[] = [ + 'type' => $json['scan_item'], + 'status' => $job['status'], + ]; +} + +$result = [ + 'reportID' => isset($final_job['results']['report_id']) ? $final_job['results']['report_id'] : null, + 'status' => $final_job['status'], + 'jobs' => $job_status, +]; + +echo(json_encode($result)); diff --git a/public/scanner.php b/public/scanner.php index 711d85840..bbf2d1658 100644 --- a/public/scanner.php +++ b/public/scanner.php @@ -1,21 +1,21 @@ . +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . * -* Primary Author Contact: Jacob Bates +* Primary Author Contact: Jacob Bates */ // This page is the landing page for LTI launches from canvas @@ -35,22 +35,22 @@ $post_input['custom_canvas_course_id'] = filter_input(INPUT_POST, 'custom_canvas_course_id', FILTER_SANITIZE_NUMBER_INT); // verify we have the variables we need from the LTI launch -$expect = ['base_url','launch_params',]; +$expect = ['base_url', 'launch_params', ]; foreach ($expect as $key) { - if(empty($_SESSION[$key])){ - UdoitUtils::instance()->exitWithPageError("Missing Session information. Please refresh the page. Missing: {$key}"); - } + if (empty($_SESSION[$key])) { + UdoitUtils::instance()->exitWithPageError("Missing Session information. Please refresh the page. Missing: {$key}"); + } } UdoitUtils::$canvas_base_url = $_SESSION['base_url']; // display the scanner page $template_data = [ - 'base_url' => $_SESSION['base_url'], - 'welcome_message' => $udoit_welcome_message, - 'disclaimer_message' => $udoit_disclaimer_message, - 'launch_params' => $_SESSION['launch_params'], - 'udoit_tests' => include(__DIR__.'/../config/tests.php') + 'base_url' => $_SESSION['base_url'], + 'welcome_message' => $udoit_welcome_message, + 'disclaimer_message' => $udoit_disclaimer_message, + 'launch_params' => $_SESSION['launch_params'], + 'udoit_tests' => include(__DIR__.'/../config/tests.php'), ]; $templates = new League\Plates\Engine(__DIR__.'/../templates'); diff --git a/tests/BaseTest.php b/tests/BaseTest.php index fbd34d418..da9867aa1 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -18,39 +18,37 @@ * Primary Author Contact: Jacob Bates */ -class MockObj { - function __call($name, $args) { - if (is_callable($this->$name)) { - array_unshift($args, $this); - return call_user_func_array($this->$name, $args); - } - } -} - -class BaseTest extends PHPUnit_Framework_TestCase{ +class BaseTest extends PHPUnit_Framework_TestCase +{ - protected static function getPrivateStaticPropertyValue($class, $prop){ + protected static function getPrivateStaticPropertyValue($class, $prop) + { $class = new ReflectionClass($class); $p = $class->getProperty($prop); $p->setAccessible(true); + return $p->getValue(); } - protected static function callProtectedStaticMethod($class, $method){ + protected static function callProtectedStaticMethod($class, $method) + { $refClass = new ReflectionClass($class); $refMethod = $refClass->getMethod($method); $refMethod->setAccessible(true); + return $refMethod->invokeArgs(null, array_slice(func_get_args(), 2)); } - protected static function setPrivateStaticPropertyValue($class, $prop, $value){ + protected static function setPrivateStaticPropertyValue($class, $prop, $value) + { $class = new ReflectionClass($class); $p = $class->getProperty($prop); $p->setAccessible(true); $p->setValue($value); } - protected static function clearMockDBConnection(){ + protected static function clearMockDBConnection() + { self::setPrivateStaticPropertyValue('UdoitDB', 'dbClass', 'PDO'); self::setPrivateStaticPropertyValue('UdoitDB', 'type', null); self::setPrivateStaticPropertyValue('UdoitDB', 'user', null); @@ -60,10 +58,13 @@ protected static function clearMockDBConnection(){ self::setPrivateStaticPropertyValue('UdoitDB', 'pdo', null); } - protected static function assertPDOStatementBindValueCalled($stmt, $key, $value){ + protected static function assertPDOStatementBindValueCalled($stmt, $key, $value) + { $found = []; foreach ($stmt->bind_value_calls as $call) { - if ($call[0] == $key) $found = $call; + if ($call[0] == $key) { + $found = $call; + } } self::assertGreaterThanOrEqual(2, count($found), "Statement not found for $key"); self::assertEquals($key, $found[0]); @@ -75,13 +76,15 @@ protected static function assertPDOStatementBindValueCalled($stmt, $key, $value) * both arguments incriment the return value on each call * First call returns $header_to_array_returns[0] and body_returns[0] * Yea, this is pretty cryptic and nested, mocking chained function calls php is gnarly + * @return Object Mock Get Object created by Request::get() */ - protected static function mockGetRequestResult(Array $header_to_array_returns, Array $body_returns){ + protected static function mockGetRequestResult(array $header_to_array_returns, array $body_returns) + { $called = -1; // mock *get()* from Httpful\Request::get() $mock_get = new MockObj(); // mock *send()* from Httpful\Request::get()->send() - $mock_get->send = $mock_send = function() use(&$called, $header_to_array_returns, $body_returns) { + $mock_get->send = $mock_send = function () use (&$called, $header_to_array_returns, $body_returns) { $called++; // mock *$response* from $response = Httpful\Request::get()->send() $mock_response = new MockObj(); @@ -90,31 +93,33 @@ protected static function mockGetRequestResult(Array $header_to_array_returns, A // mock *headers* from Httpful\Request::get()->send()->headers $mock_response->headers = new MockObj(); // mock *toArray()* from Httpful\Request::get()->send()->headers->toArray() - $mock_response->headers->toArray = function() use(&$called, $header_to_array_returns) { + $mock_response->headers->toArray = function () use (&$called, $header_to_array_returns) { return $header_to_array_returns[$called]; }; $mock_response->body = $body_returns[$called]; + return $mock_response; - }; + }; - // mock *followRedirects()* on Httpful\Request::get()->followRedirects() - $mock_get->followRedirects = function() use(&$mock_send ) { + // mock *followRedirects()* on Httpful\Request::get()->followRedirects() + $mock_get->followRedirects = function () use (&$mock_send) { $m = new MockObj(); // mock *expectsHtml()* on Httpful\Request::get()->followRedirects()->expectsHtml() - $m->expectsHtml = function() use(&$mock_send) { + $m->expectsHtml = function () use (&$mock_send) { // mock *send()* on Httpful\Request::get()->followRedirects()->expectsHtml()->send() $m = new MockObj(); $m->send = $mock_send; + return $m; }; + return $m; }; - $mock_get->addHeader = function() use(&$mock_get){ + $mock_get->addHeader = function () use (&$mock_get) { return $mock_get; }; - return $mock_get; + return $mock_get; } - } diff --git a/tests/FunctionalTests.php b/tests/FunctionalTests.php index 2cdc27a5a..5d865e182 100644 --- a/tests/FunctionalTests.php +++ b/tests/FunctionalTests.php @@ -30,7 +30,8 @@ class FunctionalTests extends PHPUnit_Framework_TestCase * @group functional * Tests the youtube api call to make sure a video with captions is detected as having captions */ - public function testYouTubeAPIHasCaptions() { + public function testYouTubeAPIHasCaptions() + { $vid_url = 'https://www.youtube.com/watch?v=zo6aRvf-l_s'; $yt_service = new youtubeService(); @@ -43,7 +44,8 @@ public function testYouTubeAPIHasCaptions() { * @group functional * Tests the youtube api call to make sure a video without captions is detected as not having captions */ - public function testYouTubeAPINoCaptions() { + public function testYouTubeAPINoCaptions() + { $vid_url = 'https://www.youtube.com/watch?v=nBH89Y0Xj7c'; $yt_service = new youtubeService(); diff --git a/tests/IndexTest.php b/tests/IndexTest.php index 5517ff3df..5eda68bda 100644 --- a/tests/IndexTest.php +++ b/tests/IndexTest.php @@ -19,18 +19,21 @@ */ class IndexTest extends BaseTest { - protected function setUp() { + protected function setUp() + { UdoitDB::setup('test', 'b', 'c', 'd'); include(__DIR__.'/../bin/db_create_tables.php'); Mockery::close(); } - protected function tearDown(){ + protected function tearDown() + { UdoitDB::disconnect(); Mockery::close(); } - public function testApiParseLinksParsesLinkHeadersCorrectly() { + public function testApiParseLinksParsesLinkHeadersCorrectly() + { // $_POST = []; // $_POST['custom_canvas_user_id'] = 'adff'; // $_POST['custom_canvas_course_id'] = 'adff'; @@ -43,4 +46,4 @@ public function testApiParseLinksParsesLinkHeadersCorrectly() { // $buffer = ob_get_clean(); // self::assertContains('UDOIT Accessibility Checker', $buffer); } -} \ No newline at end of file +} diff --git a/tests/MockObj.php b/tests/MockObj.php new file mode 100644 index 000000000..9b4cc34d8 --- /dev/null +++ b/tests/MockObj.php @@ -0,0 +1,31 @@ +. +* +* Primary Author Contact: Jacob Bates +*/ + +class MockObj +{ + public function __call($name, $args) + { + if (is_callable($this->$name)) { + array_unshift($args, $this); + + return call_user_func_array($this->$name, $args); + } + } +} diff --git a/tests/PDOMock.php b/tests/PDOMock.php new file mode 100644 index 000000000..73ba276e0 --- /dev/null +++ b/tests/PDOMock.php @@ -0,0 +1,77 @@ +. +* +* Primary Author Contact: Jacob Bates +*/ + +class PDOMock extends \PDO +{ + + public $constructor_args = []; + public $query_calls = []; + public $prepare_calls = []; + public $begin_transaction_calls = []; + public $commit_calls = []; + public $statements = []; + public $query_returns_data = false; + public static $next_fetch_return = null; + + public function __construct() + { + $this->constructor_args = func_get_args(); + } + + public function query() + { + $this->query_calls[] = func_get_args(); + if ($this->query_returns_data) { + return self::$next_fetch_return; + } + + return $this->_newStatement(); + } + + public function prepare($statement, $options = null) + { + $this->prepare_calls[] = func_get_args(); + + return $this->_newStatement(); + } + + public function beginTransaction() + { + $this->begin_transaction_calls[] = func_get_args(); + } + + public function commit() + { + $this->commit_calls[] = func_get_args(); + } + + public function nextFetchReturns($value) + { + self::$next_fetch_return = $value; + } + + protected function _newStatement() + { + $stmt = new PDOStatementMock(); + $this->statements[] = $stmt; + + return $stmt; + } +} diff --git a/tests/PDOStatementMock.php b/tests/PDOStatementMock.php new file mode 100644 index 000000000..6b242131b --- /dev/null +++ b/tests/PDOStatementMock.php @@ -0,0 +1,61 @@ +. +* +* Primary Author Contact: Jacob Bates +*/ + +class PDOStatementMock extends \PDOStatement +{ + + public $bind_value_calls = []; + public $execute_calls = []; + + public function bindValue($paramno, $param, $type = null) + { + $this->bind_value_calls[] = func_get_args(); + } + + public function execute($bound_input_params = null) + { + $this->execute_calls[] = func_get_args(); + + return true; + } + + public function fetchColumn($column_number = null) + { + return $this->mockFetch(); + } + + public function fetchObject($class_name = null, $ctor_args = null) + { + return $this->mockFetch(); + } + + public function next() + { + return $this->mockFetch(); + } + + protected function mockFetch() + { + $val = PDOMock::$next_fetch_return; + PDOMock::$next_fetch_return = null; + + return $val; + } +} diff --git a/tests/UdoitDBTest.php b/tests/UdoitDBTest.php index 197e7ea56..6451b3987 100644 --- a/tests/UdoitDBTest.php +++ b/tests/UdoitDBTest.php @@ -18,94 +18,27 @@ * Primary Author Contact: Jacob Bates */ -class PDOStatementMock extends \PDOStatement { +class UdoitDBTest extends BaseTest +{ - public $bind_value_calls = []; - public $execute_calls = []; - - public function bindValue($paramno, $param, $type = NULL){ - $this->bind_value_calls[] = func_get_args(); - } - - public function execute($bound_input_params = NULL){ - $this->execute_calls[] = func_get_args(); - return true; - } - - public function fetchColumn($column_number = NULL){ - return $this->mockFetch(); - } - - public function fetchObject($class_name = NULL, $ctor_args = NULL){ - return $this->mockFetch(); - } - - protected function mockFetch(){ - $val = PDOMock::$next_fetch_return; - PDOMock::$next_fetch_return = null; - return $val; - } - - public function next() { - return $this->mockFetch(); - } -} - -class PDOMock extends \PDO { - public $constructor_args = []; - public $query_calls = []; - public $prepare_calls = []; - public $begin_transaction_calls = []; - public $commit_calls = []; - public $statements = []; - public $query_returns_data = false; - public static $next_fetch_return = null; - - public function __construct() { - $this->constructor_args = func_get_args(); - } - - public function query(){ - $this->query_calls[] = func_get_args(); - if($this->query_returns_data) return self::$next_fetch_return; - return $this->_newStatement(); - } - - public function prepare($statement, $options = NULL){ - $this->prepare_calls[] = func_get_args(); - return $this->_newStatement(); - } - - public function beginTransaction(){ - $this->begin_transaction_calls[] = func_get_args(); - } - - public function commit(){ - $this->commit_calls[] = func_get_args(); + public static function setUpBeforeClass() + { + require_once(__DIR__.'/PDOMock.php'); + require_once(__DIR__.'/PDOStatementMock.php'); } - public function nextFetchReturns($value){ - self::$next_fetch_return = $value; - } - - protected function _newStatement(){ - $stmt = new PDOStatementMock(); - $this->statements[] = $stmt; - return $stmt; - } -} - -class UdoitDBTest extends BaseTest{ - - protected function setUp() { + public function setUp() + { self::setPrivateStaticPropertyValue('UdoitDB', 'dbClass', 'PDOMock'); } - protected function tearDown(){ + public function tearDown() + { self::clearMockDBConnection(); } - public function testMysqlSetup(){ + public function testMysqlSetup() + { UdoitDB::setup('mysql', 'b', 'c', 'd'); self::assertEquals('mysql', self::getPrivateStaticPropertyValue('UdoitDB', 'type')); @@ -114,7 +47,8 @@ public function testMysqlSetup(){ self::assertEquals('d', self::getPrivateStaticPropertyValue('UdoitDB', 'password')); } - public function testPsqlSetup(){ + public function testPsqlSetup() + { UdoitDB::setup('pgsql', 'b', 'c', 'd'); self::assertEquals('pgsql', self::getPrivateStaticPropertyValue('UdoitDB', 'type')); @@ -123,7 +57,8 @@ public function testPsqlSetup(){ self::assertEquals('d', self::getPrivateStaticPropertyValue('UdoitDB', 'password')); } - public function testConnectMysql(){ + public function testConnectMysql() + { UdoitDB::setup('mysql', 'b', 'c', 'd'); UdoitDB::testAndReconnect(); $pdo = self::getPrivateStaticPropertyValue('UdoitDB', 'pdo'); @@ -131,7 +66,8 @@ public function testConnectMysql(){ self::assertArraySubset(['b', 'c', 'd'], $pdo->constructor_args); } - public function testConnectPgsql(){ + public function testConnectPgsql() + { UdoitDB::setup('pgsql', 'b', 'c', 'd'); UdoitDB::testAndReconnect(); $pdo = self::getPrivateStaticPropertyValue('UdoitDB', 'pdo'); @@ -139,7 +75,8 @@ public function testConnectPgsql(){ self::assertArraySubset(['b'], $pdo->constructor_args); } - public function testDisconnect(){ + public function testDisconnect() + { UdoitDB::setup('mysql', 'b', 'c', 'd'); UdoitDB::testAndReconnect(); self::assertInstanceOf(PDOMock, self::getPrivateStaticPropertyValue('UdoitDB', 'pdo')); @@ -147,12 +84,14 @@ public function testDisconnect(){ self::assertNull(self::getPrivateStaticPropertyValue('UdoitDB', 'pdo')); } - public function testConnectionTestWithoutConnection(){ + public function testConnectionTestWithoutConnection() + { UdoitDB::setup('mysql', 'b', 'c', 'd'); self::assertFalse(UdoitDB::test()); } - public function testConnectionTestWithConnectionBeforeTimeoutDoesntRunQuery(){ + public function testConnectionTestWithConnectionBeforeTimeoutDoesntRunQuery() + { UdoitDB::setup('mysql', 'b', 'c', 'd'); self::callProtectedStaticMethod('UdoitDB', 'connect'); @@ -161,7 +100,8 @@ public function testConnectionTestWithConnectionBeforeTimeoutDoesntRunQuery(){ self::assertEmpty($pdo->query_calls); } - public function testConnectionTestWithConnectionBeforeTimeoutWithForceOnDoesRunQuery(){ + public function testConnectionTestWithConnectionBeforeTimeoutWithForceOnDoesRunQuery() + { UdoitDB::setup('mysql', 'b', 'c', 'd'); self::callProtectedStaticMethod('UdoitDB', 'connect'); @@ -170,7 +110,8 @@ public function testConnectionTestWithConnectionBeforeTimeoutWithForceOnDoesRunQ self::assertCount(1, $pdo->query_calls); } - public function testConnectionTestWithConnectionAfterTimeoutDoesRunQuery(){ + public function testConnectionTestWithConnectionAfterTimeoutDoesRunQuery() + { UdoitDB::setup('mysql', 'b', 'c', 'd'); self::callProtectedStaticMethod('UdoitDB', 'connect'); self::setPrivateStaticPropertyValue('UdoitDB', 'last_test_time', 0); @@ -180,7 +121,8 @@ public function testConnectionTestWithConnectionAfterTimeoutDoesRunQuery(){ self::assertCount(1, $pdo->query_calls); } - public function testPDOPassThroughCallsPDOFunction(){ + public function testPDOPassThroughCallsPDOFunction() + { UdoitDB::setup('mysql', 'b', 'c', 'd'); self::callProtectedStaticMethod('UdoitDB', 'connect'); @@ -189,13 +131,12 @@ public function testPDOPassThroughCallsPDOFunction(){ self::assertEquals('QUERY VALUE HERE', $pdo->query_calls[0][0]); } - public function testPDOPassThroughWillReconnect(){ + public function testPDOPassThroughWillReconnect() + { UdoitDB::setup('mysql', 'b', 'c', 'd'); self::assertFalse(UdoitDB::test()); UdoitDB::query('QUERY VALUE HERE'); self::assertTrue(UdoitDB::test()); } - } - diff --git a/tests/UdoitJobTest.php b/tests/UdoitJobTest.php index 9c29c3b45..bd7f715ab 100644 --- a/tests/UdoitJobTest.php +++ b/tests/UdoitJobTest.php @@ -17,36 +17,40 @@ * * Primary Author Contact: Jacob Bates */ -use org\bovigo\vfs\vfsStream; -use org\bovigo\vfs\vfsStreamDirectory; -class UdoitJobTest extends BaseTest{ - - protected function setUp() { - $this->exampleDir = vfsStream::setup('exampleDir'); // this resets after every test +class UdoitJobTest extends BaseTest +{ + public function setUp() + { Mockery::close(); UdoitDB::setup('test', 'b', 'c', 'd'); include(__DIR__.'/../bin/db_create_tables.php'); } - protected function tearDown(){ + public function tearDown() + { self::setPrivateStaticPropertyValue('UdoitUtils', 'instance', null); UdoitDB::disconnect(); Mockery::close(); } // mock UdoitUtils->getValidRefreshedApiKey() - protected function mockGetValidRefreshKey(){ + public function mockGetValidRefreshKey() + { $mock = new MockObj(); - $mock->getValidRefreshedApiKey = function(){ return 'test_api_key'; }; + $mock->getValidRefreshedApiKey = function () { + return 'test_api_key'; + }; self::setPrivateStaticPropertyValue('UdoitUtils', 'instance', $mock); } - public function testCreateJobGroupID(){ + public function testCreateJobGroupID() + { self::assertStringStartsWith('job_', UdoitJob::createJobGroupId()); } - public function testAddJobToQueue(){ + public function testAddJobToQueue() + { UdoitJob::$background_worker_enabled = true; UdoitJob::addJobToQueue('type', 55, 'job_4', ['data' => 'data_value']); @@ -59,7 +63,8 @@ public function testAddJobToQueue(){ self::assertTrue(empty($res['results'])); } - public function testAddJobToQueueRunsNextJob(){ + public function testAddJobToQueueRunsNextJob() + { self::mockGetValidRefreshKey(); // create a user (id will be 1) @@ -80,7 +85,8 @@ public function testAddJobToQueueRunsNextJob(){ self::assertTrue(!empty($res['results'])); } - public function testCountJobsRemaining(){ + public function testCountJobsRemaining() + { UdoitJob::$background_worker_enabled = true; self::assertEquals(0, UdoitJob::countJobsRemaining()); @@ -92,7 +98,8 @@ public function testCountJobsRemaining(){ self::assertEquals(2, UdoitJob::countJobsRemaining()); } - public function testGetNextJobChangesStatusToRunning(){ + public function testGetNextJobChangesStatusToRunning() + { UdoitJob::$background_worker_enabled = true; UdoitJob::addJobToQueue('type', 11, 'job_4', ['data' => 'data_value']); UdoitJob::addJobToQueue('type', 22, 'job_5', ['data' => 'data_value']); @@ -104,17 +111,18 @@ public function testGetNextJobChangesStatusToRunning(){ // make sure it gets the next job because the first is now running $job = self::callProtectedStaticMethod('UdoitJob', 'getNextJob'); self::assertEquals(22, $job->user_id); - } - public function testGetNextJobHandlesNoJobsGracefully(){ + public function testGetNextJobHandlesNoJobsGracefully() + { UdoitJob::$background_worker_enabled = true; $job = self::callProtectedStaticMethod('UdoitJob', 'getNextJob'); self::assertFalse($job); } - public function testFinalizeReportProcessesJobsAndWritesReportToDB(){ + public function testFinalizeReportProcessesJobsAndWritesReportToDB() + { self::mockGetValidRefreshKey(); // create a user (id will be 1) UdoitDB::query("INSERT into users (api_key, refresh_token) VALUES ('sample_api_key', 'refresh_token')"); @@ -145,7 +153,8 @@ public function testFinalizeReportProcessesJobsAndWritesReportToDB(){ } - public function testUpdateJobStatus(){ + public function testUpdateJobStatus() + { self::mockGetValidRefreshKey(); UdoitDB::query("INSERT into users (api_key, refresh_token) VALUES ('sample_api_key', 'refresh_token')"); UdoitJob::$background_worker_enabled = false; @@ -159,7 +168,8 @@ public function testUpdateJobStatus(){ self::assertEquals('broken', $jobs[0]['status']); } - public function testFinalizeProperlyCombinesJobResults(){ + public function testFinalizeProperlyCombinesJobResults() + { self::mockGetValidRefreshKey(); UdoitDB::query("INSERT into users (api_key, refresh_token) VALUES ('sample_api_key', 'refresh_token')"); UdoitJob::$background_worker_enabled = false; @@ -219,5 +229,4 @@ public function testFinalizeProperlyCombinesJobResults(){ self::assertArrayHasKey('items', $report['content']['module_urls']); self::assertCount(2, $report['content']['module_urls']['items']); } - } diff --git a/tests/UdoitTest.php b/tests/UdoitTest.php index 9d82c9fb0..a9e52487a 100644 --- a/tests/UdoitTest.php +++ b/tests/UdoitTest.php @@ -18,23 +18,21 @@ * Primary Author Contact: Jacob Bates */ -use org\bovigo\vfs\vfsStream; -use org\bovigo\vfs\vfsStreamDirectory; - -class UdoitTest extends BaseTest{ - - protected function setUp() { - $this->exampleDir = vfsStream::setup('exampleDir'); // this resets after every test +class UdoitTest extends BaseTest +{ + public function setUp() + { Mockery::close(); } - protected function tearDown(){ + public function tearDown() + { UdoitDB::disconnect(); Mockery::close(); } - public function testApiParseLinksParsesLinkHeadersCorrectly() { - + public function testApiParseLinksParsesLinkHeadersCorrectly() + { $link_header = '; rel="current",'. '; rel="next",'. '; rel="first",'. @@ -48,18 +46,19 @@ public function testApiParseLinksParsesLinkHeadersCorrectly() { self::assertEquals('https://url.com/last', $result['last']); } - public function testApiGetAllLinksFollowsLinkHeadersAndReturnsExpectedResults() { + public function testApiGetAllLinksFollowsLinkHeadersAndReturnsExpectedResults() + { $header_to_array_returns = [ ['link' => '; rel="next"'], // first call ['link' => '; rel="next"'], // second call - ['link' => ''] + ['link' => ''], ]; $body_returns = [ ["body_0_a", "body_0_b"], ["body_1_a", "body_1_b"], - ["body_2_a", "body_2_b"] + ["body_2_a", "body_2_b"], ]; $mock_get_result = self::mockGetRequestResult($header_to_array_returns, $body_returns); @@ -73,14 +72,14 @@ public function testApiGetAllLinksFollowsLinkHeadersAndReturnsExpectedResults() self::assertCount(6, $result); self::assertArraySubset(['body_0_a', 'body_0_b', 'body_1_a', 'body_1_b', 'body_2_a', 'body_2_b'], $result); - } - public function testGetCoursContentGetsMultipleItems(){ + public function testGetCoursContentGetsMultipleItems() + { $header_to_array_returns = [ ['link' => '; rel="next"'], // first call ['link' => '; rel="next"'], // second call - ['link' => ''] + ['link' => ''], ]; $body_returns = [ @@ -89,18 +88,18 @@ public function testGetCoursContentGetsMultipleItems(){ 'id' => "id_value", 'message' => 'message_value', 'title' => 'title_value', - 'html_url' => 'url_value' - ] + 'html_url' => 'url_value', + ], ], [ (object) [ 'id' => "id_value2", 'message' => 'message_value2', 'title' => 'title_value2', - 'html_url' => 'url_value2' - ] + 'html_url' => 'url_value2', + ], ], - [] + [], ]; $mock_get_result = self::mockGetRequestResult($header_to_array_returns, $body_returns); @@ -115,10 +114,10 @@ public function testGetCoursContentGetsMultipleItems(){ self::assertCount(2, $result['items']); self::assertEquals('id_value', $result['items'][0]['id']); self::assertEquals('id_value2', $result['items'][1]['id']); - } - public function testGetCoursContentBuildsExpectedReturnStructure(){ + public function testGetCoursContentBuildsExpectedReturnStructure() + { $body_returns = [[]]; $mock_get_result = self::mockGetRequestResult([], $body_returns); @@ -141,17 +140,18 @@ public function testGetCoursContentBuildsExpectedReturnStructure(){ self::assertEmpty($result['items']); } - public function testGetCoursContentGetsAnnouncements(){ + public function testGetCoursContentGetsAnnouncements() + { $body_returns = [ [ (object) [ 'id' => "id_value", 'message' => 'message_value', 'title' => 'title_value', - 'html_url' => 'url_value' - ] + 'html_url' => 'url_value', + ], ], - [] + [], ]; $mock_get_result = self::mockGetRequestResult([], $body_returns); @@ -178,17 +178,18 @@ public function testGetCoursContentGetsAnnouncements(){ self::assertEquals('url_value', $result['items'][0]['url']); } - public function testGetCoursContentGetsAssignments(){ + public function testGetCoursContentGetsAssignments() + { $body_returns = [ [ (object) [ 'id' => "id_value", 'description' => 'description_value', 'name' => 'name_value', - 'html_url' => 'url_value' - ] + 'html_url' => 'url_value', + ], ], - [] + [], ]; $mock_get_result = self::mockGetRequestResult([], $body_returns); @@ -215,17 +216,18 @@ public function testGetCoursContentGetsAssignments(){ self::assertEquals('url_value', $result['items'][0]['url']); } - public function testGetCoursContentGetsDiscussions(){ + public function testGetCoursContentGetsDiscussions() + { $body_returns = [ [ (object) [ 'id' => "id_value", 'message' => 'message_value', 'title' => 'title_value', - 'html_url' => 'url_value' - ] + 'html_url' => 'url_value', + ], ], - [] + [], ]; $mock_get_result = self::mockGetRequestResult([], $body_returns); @@ -253,9 +255,10 @@ public function testGetCoursContentGetsDiscussions(){ } - public function testGetCoursContentGetsUnscannableFiles(){ - $header_to_array_returns = [ - ['link' => ''] // just one page + public function testGetCoursContentGetsUnscannableFiles() + { + $header_to_array_returns = [ + ['link' => ''], // just one page ]; $body_returns = [ @@ -265,10 +268,10 @@ public function testGetCoursContentGetsUnscannableFiles(){ 'filename' => "filename.pdf", 'id' => 'id_value', 'url' => 'url_value', - 'html_url' => 'url_value' - ] + 'html_url' => 'url_value', + ], ], - [] + [], ]; $mock_get_result = self::mockGetRequestResult($header_to_array_returns, $body_returns); @@ -291,9 +294,10 @@ public function testGetCoursContentGetsUnscannableFiles(){ self::assertEquals('url_value', $result['unscannable'][0]['url']); } - public function testGetCoursContentGetsFiles(){ - $header_to_array_returns = [ - ['link' => ''] // just one page + public function testGetCoursContentGetsFiles() + { + $header_to_array_returns = [ + ['link' => ''], // just one page ]; $body_returns = [ @@ -303,11 +307,11 @@ public function testGetCoursContentGetsFiles(){ 'filename' => "filename.html", 'id' => 'id_value', 'url' => 'url_value', - 'html_url' => 'url_value' - ] + 'html_url' => 'url_value', + ], ], '

some html

', - [] + [], ]; $mock_get_result = self::mockGetRequestResult($header_to_array_returns, $body_returns); @@ -335,9 +339,10 @@ public function testGetCoursContentGetsFiles(){ } - public function testGetCoursContentGetsPages(){ - $header_to_array_returns = [ - ['link' => ''] // just one page + public function testGetCoursContentGetsPages() + { + $header_to_array_returns = [ + ['link' => ''], // just one page ]; $body_returns = [ @@ -347,17 +352,16 @@ public function testGetCoursContentGetsPages(){ 'filename' => "filename.html", 'id' => 'id_value', 'url' => 'url_value', - 'html_url' => 'url_value' - ] + 'html_url' => 'url_value', + ], ], (object) [ 'url' => 'url_value', 'body' => 'body_value', 'title' => 'title_value', 'html_url' => 'html_url_value', - ] - , - [] + ], + [], ]; $mock_get_result = self::mockGetRequestResult($header_to_array_returns, $body_returns); @@ -384,9 +388,10 @@ public function testGetCoursContentGetsPages(){ self::assertEquals('html_url_value', $result['items'][0]['url']); } - public function testGetCoursContentFindsNoVieosInModulesCorrectly(){ - $header_to_array_returns = [ - ['link' => ''] // just one page + public function testGetCoursContentFindsNoVieosInModulesCorrectly() + { + $header_to_array_returns = [ + ['link' => ''], // just one page ]; $body_returns = [ @@ -397,12 +402,12 @@ public function testGetCoursContentFindsNoVieosInModulesCorrectly(){ 'external_url' => "external_url_value", 'id' => 'id_value', 'url' => 'url_value', - 'title' => 'title_value' - ] - ] - ] + 'title' => 'title_value', + ], + ], + ], ], - [] + [], ]; $mock_get_result = self::mockGetRequestResult($header_to_array_returns, $body_returns); @@ -419,9 +424,10 @@ public function testGetCoursContentFindsNoVieosInModulesCorrectly(){ self::assertEmpty($result['items']); } - public function testGetCoursContentFindsVideosInModules(){ - $header_to_array_returns = [ - ['link' => ''] // just one page + public function testGetCoursContentFindsVideosInModules() + { + $header_to_array_returns = [ + ['link' => ''], // just one page ]; $body_returns = [ @@ -432,24 +438,24 @@ public function testGetCoursContentFindsVideosInModules(){ 'external_url' => "external_url_value", 'id' => 'id_value', 'html_url' => 'html_url_value', - 'title' => 'title_value' + 'title' => 'title_value', ], (object) [ 'external_url' => "youtube_external_url_value", 'id' => 'id_value', 'html_url' => 'html_url_value', - 'title' => 'title_value' + 'title' => 'title_value', ], (object) [ 'external_url' => "vimeo_external_url_value", 'id' => 'id_value', 'html_url' => 'html_url_value', - 'title' => 'title_value' - ] - ] - ] + 'title' => 'title_value', + ], + ], + ], ], - [] + [], ]; $mock_get_result = self::mockGetRequestResult($header_to_array_returns, $body_returns); @@ -482,17 +488,18 @@ public function testGetCoursContentFindsVideosInModules(){ self::assertEquals('html_url_value', $m_urls[0]['url']); } - public function testGetCoursContentGetsSyllabus(){ - $header_to_array_returns = []; + public function testGetCoursContentGetsSyllabus() + { + $header_to_array_returns = []; $body_returns = [ [ (object) [ 'syllabus_body' => "content_value", 'id' => 'id_value', - ] + ], ], - [] + [], ]; $mock_get_result = self::mockGetRequestResult($header_to_array_returns, $body_returns); @@ -520,7 +527,8 @@ public function testGetCoursContentGetsSyllabus(){ self::assertEquals('apiurl/courses/course/assignments/syllabus', $result['items'][0]['url']); } - public function testScanContentReturnsNothingWithNoCententToScan(){ + public function testScanContentReturnsNothingWithNoCententToScan() + { $content_items = [ ['content' => ''], ]; @@ -528,13 +536,15 @@ public function testScanContentReturnsNothingWithNoCententToScan(){ self::assertEmpty($result); } - public function testScanContentReturnsNothingWithNoItems(){ + public function testScanContentReturnsNothingWithNoItems() + { $content_items = []; $result = Udoit::scanContent($content_items); self::assertEmpty($result); } - public function testScanContentReportsErrors(){ + public function testScanContentReportsErrors() + { $content_items = [ ['content' => ''], ['content' => ''], @@ -580,7 +590,8 @@ public function testScanContentReportsErrors(){ self::assertEquals('imgHasAlt', $result[0]['suggestion'][0]['type']); } - public function testRetrieveAndScanBuildsBasicResponse(){ + public function testRetrieveAndScanBuildsBasicResponse() + { $body_returns = [[]]; $mock_get_result = self::mockGetRequestResult([], $body_returns); @@ -605,9 +616,10 @@ public function testRetrieveAndScanBuildsBasicResponse(){ self::assertEquals(0, $result['amount']); } - public function testRetrieveAndScanReturnsFiles(){ - $header_to_array_returns = [ - ['link' => ''] // just one page + public function testRetrieveAndScanReturnsFiles() + { + $header_to_array_returns = [ + ['link' => ''], // just one page ]; $body_returns = [ @@ -617,18 +629,18 @@ public function testRetrieveAndScanReturnsFiles(){ 'filename' => "filename.html", 'id' => 'id_value', 'url' => 'url_value', - 'html_url' => 'url_value' + 'html_url' => 'url_value', ], (object) [ 'display_name' => "display_name_value.pdf", 'filename' => "filename.pdf", 'id' => 'id_value', 'url' => 'url_value', - 'html_url' => 'url_value' - ] + 'html_url' => 'url_value', + ], ], '', - [] + [], ]; $mock_get_result = self::mockGetRequestResult($header_to_array_returns, $body_returns); @@ -671,13 +683,14 @@ public function testRetrieveAndScanReturnsFiles(){ self::assertArrayHasKey('warning', $res['files']['items'][0]); self::assertArrayHasKey('suggestion', $res['files']['items'][0]); - self::assertCount(1 , $res['files']['items'][0]['suggestion']); - self::assertCount(1 , $res['files']['items'][0]['suggestion']); + self::assertCount(1, $res['files']['items'][0]['suggestion']); + self::assertCount(1, $res['files']['items'][0]['suggestion']); } - public function testRetrieveAndScanReturnsModules(){ - $header_to_array_returns = [ - ['link' => ''] // just one page + public function testRetrieveAndScanReturnsModules() + { + $header_to_array_returns = [ + ['link' => ''], // just one page ]; $body_returns = [ @@ -688,24 +701,24 @@ public function testRetrieveAndScanReturnsModules(){ 'external_url' => "external_url_value", 'id' => 'id_value', 'html_url' => 'html_url_value', - 'title' => 'title_value' + 'title' => 'title_value', ], (object) [ 'external_url' => "youtube_external_url_value", 'id' => 'id_value', 'html_url' => 'html_url_value', - 'title' => 'title_value' + 'title' => 'title_value', ], (object) [ 'external_url' => "vimeo_external_url_value", 'id' => 'id_value', 'html_url' => 'html_url_value', - 'title' => 'title_value' - ] - ] - ] + 'title' => 'title_value', + ], + ], + ], ], - [] + [], ]; $mock_get_result = self::mockGetRequestResult($header_to_array_returns, $body_returns); @@ -733,6 +746,4 @@ public function testRetrieveAndScanReturnsModules(){ self::assertArrayHasKey('url', $res['module_urls']['0']); self::assertArrayHasKey('title', $res['module_urls']['0']); } - } - diff --git a/tests/UfixitTest.php b/tests/UfixitTest.php index a211419b4..1121ec0aa 100644 --- a/tests/UfixitTest.php +++ b/tests/UfixitTest.php @@ -21,25 +21,29 @@ class UfixitTest extends BaseTest { protected $data; - public function setUp () { + public function setUp() + { $this->data = [ - 'api_key' => '', - 'base_uri' => '', - 'content_id' => '', - 'course_id' => '' + 'api_key' => '', + 'base_uri' => '', + 'content_id' => '', + 'course_id' => '', ]; } - public function tearDown () { + public function tearDown() + { unset($data); } - private function checkOutputBuffer() { + public function checkOutputBuffer() + { $buffer = ob_get_clean(); $this->assertEquals('', $buffer); } - public function testFixAltText() { + public function testFixAltText() + { $error_html = ''; $new_content = 'test'; $expected = 'test'; @@ -52,7 +56,8 @@ public function testFixAltText() { $this->checkOutputBuffer(); } - public function testFixCssColorOneColorBold() { + public function testFixCssColorOneColorBold() + { $error_html = 'Bad Contrasting Text'; $new_content = ['#ffffff', '#000000']; $expected = 'Bad Contrasting Text'; @@ -67,7 +72,8 @@ public function testFixCssColorOneColorBold() { $this->checkOutputBuffer(); } - public function testFixCssColorTwoColorsItalic() { + public function testFixCssColorTwoColorsItalic() + { $error_html = 'Bad Contrasting Text'; $new_content = ['#000000', '#fffff0']; $expected = 'Bad Contrasting Text'; @@ -82,7 +88,8 @@ public function testFixCssColorTwoColorsItalic() { $this->checkOutputBuffer(); } - public function testFixCssColorTwoColorsBoldItalic() { + public function testFixCssColorTwoColorsBoldItalic() + { $error_html = 'Bad Contrasting Text'; $new_content = ['#000000', '#fffff0']; $expected = 'Bad Contrasting Text'; @@ -97,7 +104,8 @@ public function testFixCssColorTwoColorsBoldItalic() { $this->checkOutputBuffer(); } - public function testFixLinkNewText() { + public function testFixLinkNewText() + { $error_html = ''; $new_content = 'test'; $expected = 'test'; @@ -110,7 +118,8 @@ public function testFixLinkNewText() { $this->checkOutputBuffer(); } - public function testFixLinkDeleteLink() { + public function testFixLinkDeleteLink() + { $error_html = ''; $new_content = ''; $expected = ''; @@ -123,7 +132,8 @@ public function testFixLinkDeleteLink() { $this->checkOutputBuffer(); } - public function testFixHeadingNewHeading() { + public function testFixHeadingNewHeading() + { $error_html = '

'; $new_content = 'Heading Text'; $expected = '

Heading Text

'; @@ -136,7 +146,8 @@ public function testFixHeadingNewHeading() { $this->checkOutputBuffer(); } - public function testFixHeadingDeleteHeading() { + public function testFixHeadingDeleteHeading() + { $error_html = '

'; $new_content = ''; $expected = ''; @@ -149,7 +160,8 @@ public function testFixHeadingDeleteHeading() { $this->checkOutputBuffer(); } - public function testFixTableHeadersRow() { + public function testFixTableHeadersRow() + { $error_html = '
Header OneHeader Two
1.304.50
'; $sel_header = 'row'; $expected = ''."\n".'Header One'."\n".'Header Two'."\n".''."\n"; @@ -162,7 +174,8 @@ public function testFixTableHeadersRow() { $this->checkOutputBuffer(); } - public function testFixTableHeadersCol() { + public function testFixTableHeadersCol() + { $error_html = '
Header OneHeader Two
Title4.50
'; $sel_header = 'col'; $expected = ''."\n".'Header One'."\n".'Header Two'."\n".''."\n".''."\n".'Title'."\n".'4.50'."\n".''; @@ -175,7 +188,8 @@ public function testFixTableHeadersCol() { $this->checkOutputBuffer(); } - public function testFixTableHeadersBoth() { + public function testFixTableHeadersBoth() + { $error_html = '
Header OneHeader Two
Title4.50
'; $sel_header = 'both'; $expected = ''."\n".'Header One'."\n".'Header Two'."\n".''."\n".''."\n".'Title'."\n".'4.50'."\n".''; @@ -188,7 +202,8 @@ public function testFixTableHeadersBoth() { $this->checkOutputBuffer(); } - public function testFixTableThScopes() { + public function testFixTableThScopes() + { $error_html = 'Heading One'; $new_content = 'col'; $expected = 'Heading One'; @@ -200,4 +215,4 @@ public function testFixTableThScopes() { $this->assertEquals($expected, $output); $this->checkOutputBuffer(); } -} \ No newline at end of file +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index ad1360ed7..c14fa6cdc 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -20,5 +20,6 @@ // add the default autoloader require_once(__DIR__.'/../vendor/autoload.php'); +require_once(__DIR__.'/MockObj.php'); require_once(__DIR__.'/BaseTest.php'); require_once(__DIR__.'/../config/settings.php');