diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 00000000..03b3fff4 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,37 @@ +# This is a basic workflow to help you get started with Actions +name: CI + +# Controls when the action will run. Triggers the workflow on push or pull request +# events but only for the master branch +on: + push: + branches: + - master + - release/* + - feature/* + pull_request: + branches: + - master + - release/* + - feature/* + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 + + - name: PHPUnit (php-actions) + uses: php-actions/phpunit@master + with: + # Configuration file location + config: forum/phpunit.xml + bootstrap: forum/qa-tests/autoload.php + # Memory limit + memory: 512M \ No newline at end of file diff --git a/.gitignore b/.gitignore index 72ef1bf5..ff645e99 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .idea/ .vagrant/ *.retry - +.phpunit.result.cache /vendor/ diff --git a/forum/phpunit.xml b/forum/phpunit.xml new file mode 100644 index 00000000..1b6b788d --- /dev/null +++ b/forum/phpunit.xml @@ -0,0 +1,27 @@ + + + + + ./qa-tests + + + + + + + + + + + + + + diff --git a/forum/qa-include/qa-base.php b/forum/qa-include/qa-base.php index c07e572e..ad41fdc5 100644 --- a/forum/qa-include/qa-base.php +++ b/forum/qa-include/qa-base.php @@ -898,27 +898,31 @@ function qa_xml($string) } - function qa_js($value, $forcequotes=false) + function qa_js($value, $forcequotes = false) /* Return JavaScript representation of $value, putting in quotes if non-numeric or if $forcequotes is true. In the case of boolean values they are returned as the appropriate true or false string */ { - $boolean = is_bool($value); - if ($boolean) - $value = $value ? 'true' : 'false'; - if ((is_numeric($value) || $boolean) && !$forcequotes) - return $value; - else - return "'".strtr($value, array( - "'" => "\\'", - '/' => '\\/', - '\\' => '\\\\', - "\n" => "\\n", - "\r" => "\\n", - ))."'"; - } + $boolean = is_bool($value); + if ($boolean) { + $value = $value ? 'true' : 'false'; + } + + if ((is_numeric($value) || $boolean) && !$forcequotes) { + return $value; + } + + $map = [ + "'" => "\\'", + '/' => '\\/', + '\\' => '\\\\', + "\n" => "\\n", + "\r" => "\\n", + ]; + return "'" . strtr($value, $map) . "'"; + } // Finding out more about the current request diff --git a/forum/qa-plugin/q2apro-warn-on-leave/q2apro-warnonleave-layer.php b/forum/qa-plugin/q2apro-warn-on-leave/q2apro-warnonleave-layer.php index c0b2a802..5c203540 100644 --- a/forum/qa-plugin/q2apro-warn-on-leave/q2apro-warnonleave-layer.php +++ b/forum/qa-plugin/q2apro-warn-on-leave/q2apro-warnonleave-layer.php @@ -28,13 +28,13 @@ class qa_html_theme_layer extends qa_html_theme_base { var $plugin_url_warnonleave; // needed to get the plugin url - function qa_html_theme_layer($template, $content, $rooturl, $request) + public function __construct($template, $content, $rooturl, $request) { if(qa_opt('q2apro_warnonleave_enabled')) { global $qa_layers; $this->plugin_url_warnonleave = $qa_layers['Warn-On-Leave Layer']['urltoroot']; } - qa_html_theme_base::qa_html_theme_base($template, $content, $rooturl, $request); + qa_html_theme_base::__construct($template, $content, $rooturl, $request); } function head_script(){ diff --git a/forum/qa-tests/AppUsersTest.php b/forum/qa-tests/AppUsersTest.php index 41d6afd9..2f68e4e0 100644 --- a/forum/qa-tests/AppUsersTest.php +++ b/forum/qa-tests/AppUsersTest.php @@ -1,8 +1,11 @@ assertSame("'test'", $test); - - $test = qa_js('test', true); - $this->assertSame("'test'", $test); +use PHPUnit\Framework\TestCase; - $test = qa_js(123); - $this->assertSame(123, $test); +class BaseTest extends TestCase +{ + /** + * @dataProvider qa_js_dataprovider + */ + public function test__qa_js($input, bool $forceQuotes, $expectedResult): void + { + $test = qa_js($input, $forceQuotes); + $this->assertSame($expectedResult, $test); + } - $test = qa_js(123, true); - $this->assertSame("'123'", $test); + /** + * @dataProvider qa_version_to_float_dataprovider + */ + public function test__qa_version_to_float($version, $expectedResult): void + { + $test = qa_version_to_float($version); + $this->assertSame($expectedResult, $test); + } - $test = qa_js(true); - $this->assertSame('true', $test); + public function qa_version_to_float_dataprovider(): array + { + return [ + ['1.0', 1.0], + ['1.6.2.2', 1.006002002], + [1.6, 1.006], + ]; + } - $test = qa_js(true, true); - $this->assertSame("'true'", $test); - } -} + public function qa_js_dataprovider(): array + { + return [ + [ 'test', false, "'test'"], + [ 'test', true, "'test'"], + [ 123, false, 123], + [ 123, true, "'123'"], + [ true, false, 'true'], + [ true, true, "'true'"], + ]; + } +} \ No newline at end of file diff --git a/forum/qa-tests/UtilStringTest.php b/forum/qa-tests/UtilStringTest.php index 1e4e889f..bb5cd484 100644 --- a/forum/qa-tests/UtilStringTest.php +++ b/forum/qa-tests/UtilStringTest.php @@ -1,50 +1,52 @@ strBasic); - $expected1 = array('so', 'i', 'tied', 'an', 'onion', 'to', 'my', 'belt', 'which', 'was', 'the', 'style', 'at', 'the', 'time'); + $expected1 = ['so', 'i', 'tied', 'an', 'onion', 'to', 'my', 'belt', 'which', 'was', 'the', 'style', 'at', 'the', 'time']; $test2 = qa_string_to_words($this->strBasic, false); - $expected2 = array('So', 'I', 'tied', 'an', 'onion', 'to', 'my', 'belt', 'which', 'was', 'the', 'style', 'at', 'the', 'time'); + $expected2 = ['So', 'I', 'tied', 'an', 'onion', 'to', 'my', 'belt', 'which', 'was', 'the', 'style', 'at', 'the', 'time']; $this->assertEquals($expected1, $test1); $this->assertEquals($expected2, $test2); } - public function test__qa_string_remove_accents() - { + public function test__qa_string_remove_accents(): void + { $test = qa_string_remove_accents($this->strAccents); $expected = 'The quicK ssroWn Fot jUOIps OVEr THE LAEzy Dog'; $this->assertEquals($expected, $test); } - public function test__qa_tags_to_tagstring() - { - $test = qa_tags_to_tagstring( array('Hello', 'World') ); + public function test__qa_tags_to_tagstring(): void + { + $test = qa_tags_to_tagstring(['Hello', 'World']); $expected = 'Hello,World'; $this->assertEquals($expected, $test); } - public function test__qa_tagstring_to_tags() - { + public function test__qa_tagstring_to_tags(): void + { $test = qa_tagstring_to_tags('hello,world'); - $expected = array('hello', 'world'); + $expected = ['hello', 'world']; $this->assertEquals($expected, $test); } - public function test__qa_shorten_string_line() - { + public function test__qa_shorten_string_line(): void + { // qa_shorten_string_line ($string, $length) $test = qa_shorten_string_line($this->strBasic, 30); @@ -54,41 +56,41 @@ public function test__qa_shorten_string_line() $this->assertNotFalse(strpos($test, '...')); } - public function test__qa_block_words_explode() - { + public function test__qa_block_words_explode(): void + { $test = qa_block_words_explode($this->blockWordString); - $expected = array('t*d', 'o*n', 'b*t', 'style'); + $expected = ['t*d', 'o*n', 'b*t', 'style']; $this->assertEquals($expected, $test); } - public function test__qa_block_words_to_preg() - { + public function test__qa_block_words_to_preg(): void + { $test = qa_block_words_to_preg($this->blockWordString); $expected = '(?<= )t[^ ]*d(?= )|(?<= )o[^ ]*n(?= )|(?<= )b[^ ]*t(?= )|(?<= )style(?= )'; $this->assertEquals($expected, $test); } - public function test__qa_block_words_match_all() - { + public function test__qa_block_words_match_all(): void + { $test1 = qa_block_words_match_all('onion belt', ''); $wordpreg = qa_block_words_to_preg($this->blockWordString); $test2 = qa_block_words_match_all('tried an ocean boat', $wordpreg); // matches are returned as array of [offset] => [length] - $expected = array( + $expected = [ 0 => 5, // tried 9 => 5, // ocean 15 => 4, // boat - ); + ]; $this->assertEmpty($test1); $this->assertEquals($expected, $test2); } - public function test__qa_block_words_replace() - { + public function test__qa_block_words_replace(): void + { $wordpreg = qa_block_words_to_preg($this->blockWordString); $test = qa_block_words_replace('tired of my ocean boat style', $wordpreg); $expected = '***** of my ***** **** *****'; @@ -96,27 +98,27 @@ public function test__qa_block_words_replace() $this->assertEquals($expected, $test); } - public function test__qa_random_alphanum() - { + public function test__qa_random_alphanum(): void + { $len = 50; $test = qa_random_alphanum($len); $this->assertEquals(strlen($test), $len); } - public function test__qa_email_validate() - { - $goodEmails = array( + public function test__qa_email_validate(): void + { + $goodEmails = [ 'hello@example.com', 'q.a@question2answer.org', 'example@newdomain.app' - ); - $badEmails = array( + ]; + $badEmails = [ 'nobody@nowhere', 'pokémon@example.com', 'email @ with spaces', 'some random string', - ); + ]; foreach ($goodEmails as $email) { $this->assertTrue( qa_email_validate($email) ); @@ -125,31 +127,31 @@ public function test__qa_email_validate() $this->assertFalse( qa_email_validate($email) ); } - public function test__qa_strlen() - { + public function test__qa_strlen(): void + { $test = qa_strlen($this->strAccents); $this->assertEquals($test, 43); } - public function test__qa_strtolower() - { + public function test__qa_strtolower(): void + { $test = qa_strtolower('hElLo WoRld'); $this->assertEquals($test, 'hello world'); } - public function test__qa_substr() - { + public function test__qa_substr(): void + { $test = qa_substr($this->strBasic, 5, 24); $this->assertEquals($test, 'tied an onion to my belt'); } - public function test__qa_string_matches_one() - { - $matches = array( 'dyed', 'shallot', 'belt', 'fashion' ); - $nonMatches = array( 'dyed', 'shallot', 'buckle', 'fashion' ); + public function test__qa_string_matches_one(): void + { + $matches = ['dyed', 'shallot', 'belt', 'fashion']; + $nonMatches = ['dyed', 'shallot', 'buckle', 'fashion']; $this->assertTrue( qa_string_matches_one($this->strBasic, $matches) ); $this->assertFalse( qa_string_matches_one($this->strBasic, $nonMatches) ); diff --git a/forum/qa-tests/phpunit-qa-config.php b/forum/qa-tests/phpunit-qa-config.php index a26b1965..6c4da903 100644 --- a/forum/qa-tests/phpunit-qa-config.php +++ b/forum/qa-tests/phpunit-qa-config.php @@ -1,10 +1,10 @@