Browse Source

Tests for schedule intervals

master
J. King 4 years ago
parent
commit
c4774da77d
  1. 2
      lib/Parser/XML/Feed.php
  2. 12
      lib/Parser/XML/Primitives/Feed.php
  3. 2
      tests/cases/AbstractParserTestCase.php
  4. 69
      tests/cases/XML/feed-rss1.yaml
  5. 41
      tests/cases/XML/feed-rss2.yaml

2
lib/Parser/XML/Feed.php

@ -144,7 +144,7 @@ class Feed implements \MensBeam\Lax\Parser\Feed {
public function getSchedule(): Schedule {
$out = new Schedule;
$out->interval = $this->getSchedIntervalRss2();
$out->interval = $this->getSchedIntervalRss1() ?? $this->getSchedIntervalRss2();
$out->skip = $this->getSchedSkipRss2();
$out->expired = $this->getExpiredPod();
if (is_null($out->expired) && (($out->skip & Schedule::DAY_ALL) == Schedule::DAY_ALL || ($out->skip & Schedule::HOUR_ALL) == Schedule::HOUR_ALL)) {

12
lib/Parser/XML/Primitives/Feed.php

@ -95,23 +95,20 @@ trait Feed {
"monthly" => ["D", 30], // 30 days
"yearly" => ["M", 12], // 12 months
][strtolower($period)];
$f = min(1, (int) $this->fetchString("sched:updateFrequency", "0*[1-9]\d*")); // a frequency of zero makes no sense
$f = max(1, (int) $this->fetchString("sched:updateFrequency", "0*[1-9]\d*")); // a frequency of zero makes no sense
// divide the period by the frequency
// FIXME: we must have an integer result because PHP (incorrectly) rejects fractional intervals
// see https://bugs.php.net/bug.php?id=53831
$n = min(1, intdiv($n, $f)); // a frequency of zero still makes no sense, so we assume at least one subdivision
$n = max(1, intdiv($n, $f)); // a frequency of zero still makes no sense, so we assume at least one subdivision
return new \DateInterval("P".(strlen($p) === 1 ? "" : $p[0]).$n.$p[-1]);
}
return null;
}
/** Computes the "skip-schedule" of an RSS feed, the set of days and hours during which a feed should not be fetched */
protected function getSchedSkipRss2(): ?int {
$out = 0;
$hours = $this->fetchString("skipHours/hour", "\d+", true) ?? [];
foreach($hours as $h) {
foreach($this->fetchString("skipHours/hour", "\d+", true) ?? [] as $h) {
$out |= [
Schedule::HOUR_0,
Schedule::HOUR_1,
@ -140,8 +137,7 @@ trait Feed {
Schedule::HOUR_0,
][(int) $h] ?? 0;
}
$days = $this->fetchString("skipDays/day", null, true) ?? [];
foreach($days as $d) {
foreach($this->fetchString("skipDays/day", null, true) ?? [] as $d) {
$out |= [
"monday" => Schedule::DAY_MON,
"tuesday" => Schedule::DAY_TUE,

2
tests/cases/AbstractParserTestCase.php

@ -90,6 +90,8 @@ class AbstractParserTestCase extends \PHPUnit\Framework\TestCase {
foreach ($v as $kk => $vv) {
if ($kk === "url") {
$f->$k->$kk = $this->makeUrl($vv);
} elseif ($kk === "interval") {
$f->$k->$kk = new \DateInterval($vv);
} else {
$f->$k->$kk = $vv;
}

69
tests/cases/XML/feed-rss1.yaml

@ -52,3 +52,72 @@ DC ID with whitespace:
format: rdf
version: '1.0'
id: 'http://example.com/'
Syndication schedule 1:
input: >
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
<channel>
<sy:updatePeriod>hourly</sy:updatePeriod>
</channel>
</rdf:RDF>
output:
format: rdf
version: '1.0'
sched:
interval: PT60M
Syndication schedule 2:
input: >
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
<channel>
<sy:updatePeriod>DAILY</sy:updatePeriod>
<sy:updateFrequency>012</sy:updateFrequency>
</channel>
</rdf:RDF>
output:
format: rdf
version: '1.0'
sched:
interval: PT2H
Syndication schedule 3:
input: >
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
<channel>
<sy:updatePeriod> WeeKLy </sy:updatePeriod>
<sy:updateFrequency>2</sy:updateFrequency>
</channel>
</rdf:RDF>
output:
format: rdf
version: '1.0'
sched:
interval: P3D
Syndication schedule 4:
input: >
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
<channel>
<sy:updatePeriod>monthly</sy:updatePeriod>
<sy:updateFrequency>3</sy:updateFrequency>
</channel>
</rdf:RDF>
output:
format: rdf
version: '1.0'
sched:
interval: P10D
Syndication schedule 5:
input: >
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
<channel>
<sy:updatePeriod>yearly</sy:updatePeriod>
<sy:updateFrequency>24</sy:updateFrequency>
</channel>
</rdf:RDF>
output:
format: rdf
version: '1.0'
sched:
interval: P1M

41
tests/cases/XML/feed-rss2.yaml

@ -38,6 +38,47 @@ Root GUID: # Any elements on the RSS2 root element should be ignored
output:
format: rss
Schedule interval 1:
input: >
<rss><channel>
<ttl>60</ttl>
</channel></rss>
output:
format: rss
sched:
interval: PT60M
Schedule interval 2:
input: >
<rss><channel>
<ttl>bogus</ttl>
<ttl>60</ttl>
</channel></rss>
output:
format: rss
sched:
interval: PT60M
Schedule interval 3:
input: >
<rss><channel>
<ttl>
0120
</ttl>
</channel></rss>
output:
format: rss
sched:
interval: PT120M
Schedule interval 4:
input: >
<rss><channel>
<ttl>0</ttl>
</channel></rss>
output:
format: rss
Skip days:
input: >
<rss><channel>

Loading…
Cancel
Save