[web-php] master: Add tests for gen_challenge() and test_answer()
| From: | Andreas Möller via GitHub | Date: | Fri, 15 Jul 2022 11:14:18 +0000 |
| Subject: | [web-php] master: Add tests for gen_challenge() and test_answer() | ||
| Groups: | php.webmaster | ||
| Request: | Send a blank email to php-webmaster+get-30408@lists.php.net to get a copy of this message | ||
Author: Andreas Möller (localheinz)
Committer: GitHub (web-flow)
Pusher: cmb69
Date: 2022-07-15T13:07:43+02:00
Commit: https://github.com/php/web-php/commit/d8717d3ab6be0a134c279c6c289f64f19c368e7b
Raw diff: https://github.com/php/web-php/commit/d8717d3ab6be0a134c279c6c289f64f19c368e7b.diff
Add tests for gen_challenge() and test_answer()
Closes GH-632.
Changed paths:
A tests/gen-challenge.phpt
A tests/test-answer.phpt
Diff:
diff --git a/tests/gen-challenge.phpt b/tests/gen-challenge.phpt
new file mode 100644
index 000000000..5738512a3
--- /dev/null
+++ b/tests/gen-challenge.phpt
@@ -0,0 +1,246 @@
+--TEST--
+gen_challenge() generates a spam challenge
+--FILE--
+<?php
+
+require_once __DIR__ . '/../manual/spam_challenge.php';
+
+srand(9001);
+
+$challenges = array_map(static function (): array {
+ [$function, $argumentOne, $argumentTwo, $question] = gen_challenge();
+
+ return [
+ 'function' => $function,
+ 'argumentOne' => $argumentOne,
+ 'argumentTwo' => $argumentTwo,
+ 'question' => $question,
+ ];
+}, range(1, 20));
+
+var_dump($challenges);
+
+?>
+--EXPECT--
+array(20) {
+ [0]=>
+ array(4) {
+ ["function"]=>
+ string(3) "min"
+ ["argumentOne"]=>
+ string(3) "two"
+ ["argumentTwo"]=>
+ string(3) "one"
+ ["question"]=>
+ string(13) "min(two, one)"
+ }
+ [1]=>
+ array(4) {
+ ["function"]=>
+ string(5) "minus"
+ ["argumentOne"]=>
+ string(4) "five"
+ ["argumentTwo"]=>
+ string(4) "five"
+ ["question"]=>
+ string(15) "five minus five"
+ }
+ [2]=>
+ array(4) {
+ ["function"]=>
+ string(5) "minus"
+ ["argumentOne"]=>
+ string(4) "four"
+ ["argumentTwo"]=>
+ string(4) "four"
+ ["question"]=>
+ string(15) "four minus four"
+ }
+ [3]=>
+ array(4) {
+ ["function"]=>
+ string(3) "min"
+ ["argumentOne"]=>
+ string(4) "nine"
+ ["argumentTwo"]=>
+ string(5) "seven"
+ ["question"]=>
+ string(16) "min(nine, seven)"
+ }
+ [4]=>
+ array(4) {
+ ["function"]=>
+ string(5) "minus"
+ ["argumentOne"]=>
+ string(5) "seven"
+ ["argumentTwo"]=>
+ string(3) "six"
+ ["question"]=>
+ string(15) "seven minus six"
+ }
+ [5]=>
+ array(4) {
+ ["function"]=>
+ string(3) "max"
+ ["argumentOne"]=>
+ string(5) "three"
+ ["argumentTwo"]=>
+ string(3) "six"
+ ["question"]=>
+ string(15) "max(three, six)"
+ }
+ [6]=>
+ array(4) {
+ ["function"]=>
+ string(3) "max"
+ ["argumentOne"]=>
+ string(3) "six"
+ ["argumentTwo"]=>
+ string(4) "five"
+ ["question"]=>
+ string(14) "max(six, five)"
+ }
+ [7]=>
+ array(4) {
+ ["function"]=>
+ string(3) "max"
+ ["argumentOne"]=>
+ string(4) "four"
+ ["argumentTwo"]=>
+ string(5) "three"
+ ["question"]=>
+ string(16) "max(four, three)"
+ }
+ [8]=>
+ array(4) {
+ ["function"]=>
+ string(3) "min"
+ ["argumentOne"]=>
+ string(3) "two"
+ ["argumentTwo"]=>
+ string(4) "nine"
+ ["question"]=>
+ string(14) "min(two, nine)"
+ }
+ [9]=>
+ array(4) {
+ ["function"]=>
+ string(4) "plus"
+ ["argumentOne"]=>
+ string(5) "eight"
+ ["argumentTwo"]=>
+ string(3) "one"
+ ["question"]=>
+ string(14) "eight plus one"
+ }
+ [10]=>
+ array(4) {
+ ["function"]=>
+ string(4) "plus"
+ ["argumentOne"]=>
+ string(5) "three"
+ ["argumentTwo"]=>
+ string(4) "five"
+ ["question"]=>
+ string(15) "three plus five"
+ }
+ [11]=>
+ array(4) {
+ ["function"]=>
+ string(3) "min"
+ ["argumentOne"]=>
+ string(5) "eight"
+ ["argumentTwo"]=>
+ string(5) "three"
+ ["question"]=>
+ string(17) "min(eight, three)"
+ }
+ [12]=>
+ array(4) {
+ ["function"]=>
+ string(3) "max"
+ ["argumentOne"]=>
+ string(4) "zero"
+ ["argumentTwo"]=>
+ string(4) "nine"
+ ["question"]=>
+ string(15) "max(zero, nine)"
+ }
+ [13]=>
+ array(4) {
+ ["function"]=>
+ string(3) "min"
+ ["argumentOne"]=>
+ string(4) "five"
+ ["argumentTwo"]=>
+ string(4) "nine"
+ ["question"]=>
+ string(15) "min(five, nine)"
+ }
+ [14]=>
+ array(4) {
+ ["function"]=>
+ string(5) "minus"
+ ["argumentOne"]=>
+ string(3) "six"
+ ["argumentTwo"]=>
+ string(4) "four"
+ ["question"]=>
+ string(14) "six minus four"
+ }
+ [15]=>
+ array(4) {
+ ["function"]=>
+ string(3) "max"
+ ["argumentOne"]=>
+ string(3) "one"
+ ["argumentTwo"]=>
+ string(3) "one"
+ ["question"]=>
+ string(13) "max(one, one)"
+ }
+ [16]=>
+ array(4) {
+ ["function"]=>
+ string(4) "plus"
+ ["argumentOne"]=>
+ string(4) "five"
+ ["argumentTwo"]=>
+ string(4) "zero"
+ ["question"]=>
+ string(14) "five plus zero"
+ }
+ [17]=>
+ array(4) {
+ ["function"]=>
+ string(5) "minus"
+ ["argumentOne"]=>
+ string(4) "nine"
+ ["argumentTwo"]=>
+ string(5) "eight"
+ ["question"]=>
+ string(16) "nine minus eight"
+ }
+ [18]=>
+ array(4) {
+ ["function"]=>
+ string(5) "minus"
+ ["argumentOne"]=>
+ string(5) "three"
+ ["argumentTwo"]=>
+ string(3) "one"
+ ["question"]=>
+ string(15) "three minus one"
+ }
+ [19]=>
+ array(4) {
+ ["function"]=>
+ string(3) "min"
+ ["argumentOne"]=>
+ string(5) "three"
+ ["argumentTwo"]=>
+ string(3) "one"
+ ["question"]=>
+ string(15) "min(three, one)"
+ }
+}
diff --git a/tests/test-answer.phpt b/tests/test-answer.phpt
new file mode 100644
index 000000000..235d68cd7
--- /dev/null
+++ b/tests/test-answer.phpt
@@ -0,0 +1,211 @@
+--TEST--
+test_answer() returns true when answer to spam challenge is valid
+--FILE--
+<?php
+
+require_once __DIR__ . '/../manual/spam_challenge.php';
+
+$answers = [
+ [
+ 'function' => 'max',
+ 'argumentOne' => 'two',
+ 'argumentTwo' => 'one',
+ 'question' => 'max(two, one)',
+ 'answer' => 'two',
+ 'isValid' => true,
+ ],
+ [
+ 'function' => 'min',
+ 'argumentOne' => 'two',
+ 'argumentTwo' => 'one',
+ 'question' => 'min(two, one)',
+ 'answer' => 'one',
+ 'isValid' => true,
+ ],
+ [
+ 'function' => 'minus',
+ 'argumentOne' => 'five',
+ 'argumentTwo' => 'five',
+ 'question' => 'minus(five, five)',
+ 'answer' => 'zero',
+ 'isValid' => true,
+ ],
+ [
+ 'function' => 'plus',
+ 'argumentOne' => 'eight',
+ 'argumentTwo' => 'one',
+ 'question' => 'plus(eight, one)',
+ 'answer' => 'nine',
+ 'isValid' => true,
+ ],
+ [
+ 'function' => 'max',
+ 'argumentOne' => 'three',
+ 'argumentTwo' => 'six',
+ 'question' => 'max(three, six)',
+ 'answer' => 'nine',
+ 'isValid' => false,
+ ],
+ [
+ 'function' => 'min',
+ 'argumentOne' => 'two',
+ 'argumentTwo' => 'nine',
+ 'question' => 'min(two, nine)',
+ 'answer' => 'seven',
+ 'isValid' => false,
+ ],
+ [
+ 'function' => 'minus',
+ 'argumentOne' => 'seven',
+ 'argumentTwo' => 'six',
+ 'question' => 'minus(seven, six)',
+ 'answer' => 'four',
+ 'isValid' => false,
+ ],
+ [
+ 'function' => 'plus',
+ 'argumentOne' => 'eight',
+ 'argumentTwo' => 'one',
+ 'question' => 'plus(eight, one)',
+ 'answer' => 'seven',
+ 'isValid' => false,
+ ],
+];
+
+$results = array_map(static function (array $answer): array {
+ $answer['isValid'] = test_answer(
+ $answer['function'],
+ $answer['argumentOne'],
+ $answer['argumentTwo'],
+ $answer['answer']
+ );
+
+ return $answer;
+}, $answers);
+
+var_dump($results);
+
+?>
+--EXPECT--
+array(8) {
+ [0]=>
+ array(6) {
+ ["function"]=>
+ string(3) "max"
+ ["argumentOne"]=>
+ string(3) "two"
+ ["argumentTwo"]=>
+ string(3) "one"
+ ["question"]=>
+ string(13) "max(two, one)"
+ ["answer"]=>
+ string(3) "two"
+ ["isValid"]=>
+ bool(true)
+ }
+ [1]=>
+ array(6) {
+ ["function"]=>
+ string(3) "min"
+ ["argumentOne"]=>
+ string(3) "two"
+ ["argumentTwo"]=>
+ string(3) "one"
+ ["question"]=>
+ string(13) "min(two, one)"
+ ["answer"]=>
+ string(3) "one"
+ ["isValid"]=>
+ bool(true)
+ }
+ [2]=>
+ array(6) {
+ ["function"]=>
+ string(5) "minus"
+ ["argumentOne"]=>
+ string(4) "five"
+ ["argumentTwo"]=>
+ string(4) "five"
+ ["question"]=>
+ string(17) "minus(five, five)"
+ ["answer"]=>
+ string(4) "zero"
+ ["isValid"]=>
+ bool(true)
+ }
+ [3]=>
+ array(6) {
+ ["function"]=>
+ string(4) "plus"
+ ["argumentOne"]=>
+ string(5) "eight"
+ ["argumentTwo"]=>
+ string(3) "one"
+ ["question"]=>
+ string(16) "plus(eight, one)"
+ ["answer"]=>
+ string(4) "nine"
+ ["isValid"]=>
+ bool(true)
+ }
+ [4]=>
+ array(6) {
+ ["function"]=>
+ string(3) "max"
+ ["argumentOne"]=>
+ string(5) "three"
+ ["argumentTwo"]=>
+ string(3) "six"
+ ["question"]=>
+ string(15) "max(three, six)"
+ ["answer"]=>
+ string(4) "nine"
+ ["isValid"]=>
+ bool(false)
+ }
+ [5]=>
+ array(6) {
+ ["function"]=>
+ string(3) "min"
+ ["argumentOne"]=>
+ string(3) "two"
+ ["argumentTwo"]=>
+ string(4) "nine"
+ ["question"]=>
+ string(14) "min(two, nine)"
+ ["answer"]=>
+ string(5) "seven"
+ ["isValid"]=>
+ bool(false)
+ }
+ [6]=>
+ array(6) {
+ ["function"]=>
+ string(5) "minus"
+ ["argumentOne"]=>
+ string(5) "seven"
+ ["argumentTwo"]=>
+ string(3) "six"
+ ["question"]=>
+ string(17) "minus(seven, six)"
+ ["answer"]=>
+ string(4) "four"
+ ["isValid"]=>
+ bool(false)
+ }
+ [7]=>
+ array(6) {
+ ["function"]=>
+ string(4) "plus"
+ ["argumentOne"]=>
+ string(5) "eight"
+ ["argumentTwo"]=>
+ string(3) "one"
+ ["question"]=>
+ string(16) "plus(eight, one)"
+ ["answer"]=>
+ string(5) "seven"
+ ["isValid"]=>
+ bool(false)
+ }
+}