Add general abstract classes for tests, starting with test classes for

RSS feeds
This commit is contained in:
2023-08-18 15:09:58 +02:00
parent 9c49afe416
commit 88458df949
3 changed files with 598 additions and 0 deletions

View File

@ -419,6 +419,44 @@ final class MD_STD {
}
/**
* Check if a URL is reachable (200) using curl.
*
* @param string $url URL to validate.
*
* @return bool
*/
public static function checkUrlIsReachable(string $url):bool {
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
throw new MDInvalidUrl("URL to check (" . $url . ") does not seem to be a valid URL");
}
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_ENCODING => "",
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 1,
CURLOPT_NOBODY => true,
CURLOPT_TIMEOUT => 5,
// It's very important to let other webmasters know who's probing their servers.
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2',
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
CURLOPT_TCP_FASTOPEN => true,
]);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code !== 200) {
return false;
}
return true;
}
/**
* Sets and returns user language based on a session cookie.
*

View File

@ -0,0 +1,60 @@
<?PHP
/**
* Test for ensuring that search RSS feeds are generated correctly.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
/**
* Tests for the manifest.
*/
abstract class MD_STD_RSS_TEST extends TestCase {
protected string $feed;
/**
* Protected function testRssFeedValidity.
*
* @return void
*/
final public function testRssFeedValidity() {
$domDoc = new DomDocument();
self::assertTrue($domDoc->loadXML($this->feed));
self::assertTrue($domDoc->schemaValidate(__DIR__ . "/../../assets/xsd/Rss2.xsd"));
unset($domDoc);
}
/**
* Checks for the availability of RSS feed links and encosures.
*
* @return void
*/
final public function testRssFeedLinksAndEnclosure() {
if (!($xmlData = simplexml_load_string($this->feed))) {
throw new Exception("Failed to load RSS feed string to SimpleXML element");
}
self::assertNotEmpty((string)$xmlData->channel->title);
self::assertTrue(MD_STD::checkUrlIsReachable((string)$xmlData->channel->image->url), "Path " . $xmlData->channel->image->url . " does not appear to be a reachable URL");
self::assertTrue(MD_STD::checkUrlIsReachable((string)$xmlData->channel->image->link), "Path " . $xmlData->channel->image->link . " does not appear to be a reachable URL");
self::assertTrue(MD_STD::checkUrlIsReachable((string)$xmlData->channel->item->link), "Path " . $xmlData->channel->item->link . " does not appear to be a reachable URL");
if (($firstEntryImg = $xmlData->channel->item->enclosure) === null) {
throw new Exception("First item does not seem to have an enclosure");
}
if (($firstEntryImgAttr = $firstEntryImg->attributes()) === null) {
throw new Exception("First enclosure does not seem to have attributes");
}
self::assertTrue(MD_STD::checkUrlIsReachable((string)$firstEntryImgAttr->url), "First enclosure does not appear to be a reachable URL (" . $firstEntryImgAttr->url . ")");
}
}