<?php
/**
* SeekQuarry/Yioop --
* Open Source Pure PHP Search Engine, Crawler, and Indexer
*
* Copyright (C) 2009 - 2026 Chris Pollett chris@pollett.org
*
* LICENSE:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* END LICENSE
*
* @author Chris Pollett chris@pollett.org
* @license https://www.gnu.org/licenses/ GPL3
* @link https://www.seekquarry.com/
* @copyright 2009 - 2026
* @filesource
*/
namespace seekquarry\yioop\tests;
use seekquarry\yioop\library\JavascriptUnitTest;
/**
* Tests that the script which works out a page's answer to a counting puzzle
* leaves alone a page that asks for none. The site adds the call to
* every page carrying a form, and a page whose form asks no puzzle has
* none of the boxes it reads. Reading a value off a box that is not
* there stopped the rest of that page's script where it stood, and the
* line that tells somebody what just happened runs in the same breath,
* so such a page went quiet rather than saying anything.
*
* @author Chris Pollett
*/
class HashCaptchaJavascriptTest extends JavascriptUnitTest
{
/**
* Checks that the call passes over a page with no puzzle boxes and
* still works one out where the boxes are there.
*
* @return string html and Javascript that checks the answer in a
* browser or, from the command line, under node
*/
public function noPuzzleOnThePageIsLeftAloneTestCase()
{
ob_start();
?>
<div id="hashCaptchaTest"></div>
<script src="../scripts/sha1.js" ></script>
<script src="../scripts/hash_captcha.js" ></script>
<script>
var unit_test_results = [];
/* Stands in for the boxes a page carries, which are looked up
by name; a page with no puzzle has none of them. */
var boxes = {};
elt = function (named) {
return boxes[named] || null;
};
var went_wrong = "";
try {
findNonce('nonce_for_string', 'random_string', 'time',
'level');
} catch (trouble) {
went_wrong = String(trouble);
}
unit_test_results.push(
{name: "a page with no puzzle boxes is passed over",
pass: went_wrong === ""});
boxes = {
random_string: {value: "a-random-string"},
time: {value: "1785987984"},
level: {value: "1"},
nonce_for_string: {
attributes: {},
setAttribute: function (named, value) {
this.attributes[named] = value;
}
}
};
went_wrong = "";
try {
findNonce('nonce_for_string', 'random_string', 'time',
'level');
} catch (trouble) {
went_wrong = String(trouble);
}
unit_test_results.push(
{name: "a page that does ask a puzzle answers it",
pass: went_wrong === "" &&
boxes.nonce_for_string.attributes.value !== undefined});
unit_test_results.push(
{name: "and the answer is put where the form will send it",
pass: boxes.nonce_for_string.attributes.type === "hidden"});
var empty_random = {
random_string: {value: ""},
time: {value: "1785987984"},
level: {value: "1"},
nonce_for_string: {
attributes: {},
setAttribute: function (named, value) {
this.attributes[named] = value;
}
}
};
boxes = empty_random;
went_wrong = "";
try {
findNonce('nonce_for_string', 'random_string', 'time',
'level');
} catch (trouble) {
went_wrong = String(trouble);
}
unit_test_results.push(
{name: "a page whose puzzle is empty is passed over too",
pass: went_wrong === "" &&
boxes.nonce_for_string.attributes.value === undefined});
</script>
<?php
$out_data = ob_get_contents();
ob_end_clean();
return $out_data;
}
}