Browse Source

Enclosure tests

master
J. King 4 years ago
parent
commit
dd73e335ca
  1. 4
      lib/Parser/Construct.php
  2. 10
      lib/Parser/JSON/Entry.php
  3. 31
      tests/cases/JSON/JSONTest.php
  4. 147
      tests/cases/JSON/entry.json

4
lib/Parser/Construct.php

@ -64,7 +64,7 @@ trait Construct {
}
protected function parseMediaType(string $type, ?Url $url = null): ?string {
if (preg_match('<^\s*([0-9a-z]+(?:/[!#$%&\'\*\+\-\.^_`|~0-9a-z]+))(?:\s|;|$)>i', $type, $match)) {
if (preg_match('<^\s*([0-9a-z]+(?:/[!#$%&\'\*\+\-\.^_`|~0-9a-z]+)?)(?:\s|;|$)>i', $type, $match)) {
/* NOTE: The pattern used here is a subset of what is
technically allowed by RFC 7231: the "type" portion
is supposed to be as general as the "subtype" portion,
@ -82,7 +82,7 @@ trait Construct {
*/
return strtolower($match[1]);
}
if ($url) {
if ($url && (strlen($url->getScheme()) && $url->host !== null)) {
$file = substr($url->getPath(), (int) strrpos($url->getPath(), "/"));
$ext = strrpos($file, ".");
if ($ext !== false) {

10
lib/Parser/JSON/Entry.php

@ -146,6 +146,16 @@ class Entry implements \JKingWeb\Lax\Parser\Entry {
public function getEnclosures(): EnclosureCollection {
$out = new EnclosureCollection;
// handle JSON Feed's special "image" key first
$img = $this->fetchUrl("image");
if ($img) {
$m = new Enclosure;
$m->url = $img;
$m->type = "image";
$m->preferred = true;
$out[] = $m;
}
// handle other attachments
foreach ($this->fetchMember("attachments", "array") ?? [] as $attachment) {
$url = $this->fetchUrl("url", $attachment);
if ($url) {

31
tests/cases/JSON/JSONTest.php

@ -34,17 +34,19 @@ namespace JKingWeb\Lax\TestCase\JSON;
*/
use JKingWeb\Lax\Date;
use JKingWeb\Lax\Feed;
use JKingWeb\Lax\Entry;
use JKingWeb\Lax\Text;
use JKingWeb\Lax\Url;
use JKingWeb\Lax\Parser\Exception;
use JKingWeb\Lax\Parser\JSON\Feed as Parser;
use JKingWeb\Lax\Person\Person;
use JKingWeb\Lax\Category\Category;
use JKingWeb\Lax\Enclosure\Enclosure;
use JKingWeb\Lax\Person\Collection as PersonCollection;
use JKingWeb\Lax\Category\Collection as CategoryCollection;
use JKingWeb\Lax\Date;
use JKingWeb\Lax\Feed;
use JKingWeb\Lax\Entry;
use JKingWeb\Lax\Text;
use JKingWeb\Lax\Url;
use JKingWeb\Lax\Enclosure\Collection as EnclosureCollection;
/**
* @covers JKingWeb\Lax\Parser\JSON\Feed<extended>
@ -66,7 +68,6 @@ class JSONTest extends \PHPUnit\Framework\TestCase {
$act = $p->parse(new Feed);
$exp = $this->makeFeed($output);
$this->assertEquals($exp, $act);
$this->assertEqualsCanonicalizing($exp, $act);
}
}
@ -130,6 +131,12 @@ class JSONTest extends \PHPUnit\Framework\TestCase {
$c[] = $this->makePerson($m);
}
$e->$k = $c;
} elseif ($k === "enclosures") {
$c = new EnclosureCollection;
foreach ($v as $m) {
$c[] = $this->makeEnclosure($m);
}
$e->$k = $c;
} elseif ($k === "categories") {
$c = new CategoryCollection;
foreach ($v as $m) {
@ -169,4 +176,16 @@ class JSONTest extends \PHPUnit\Framework\TestCase {
}
return $p;
}
protected function makeEnclosure(\stdClass $enclosure): Enclosure {
$e = new Enclosure;
foreach ($enclosure as $k => $v) {
if ($k === "urli") {
$e->$k = new Url($v);
} else {
$e->$k = $v;
}
}
return $e;
}
}

147
tests/cases/JSON/entry.json

@ -477,5 +477,152 @@
{"id": "5", "people": [{"name": "Jane Doe", "role": "author"},{"name": "John Doe", "role": "author"}]}
]
}
},
{
"description": "Entry image",
"input": {
"version": "https://jsonfeed.org/version/1",
"items": [{
"id": "1",
"image": "http://example.com/image"
}]
},
"output": {
"format": "json",
"version": "1",
"entries": [
{
"id": "1",
"enclosures": [
{
"type": "image",
"url": "http://example.com/image",
"preferred": true
}
]
}
]
}
},
{
"description": "Entry attachments",
"input": {
"version": "https://jsonfeed.org/version/1",
"items": [{
"id": "1",
"attachments": [
{
"url": "http://example.com/image",
"mime_type": "image/svg+xml; charset=\"urf-8\"",
"title": "Logo",
"size_in_bytes": 2345
},
{
"url": "http://example.com/graphic.png"
},
{
"url": "http://example.com/graphic.PNG"
},
{
"url": "data:text/plain,Hello%20World!",
"title": "Example text"
},
{
"url": "data:text/plain,File:example.jpg",
"title": "Sneaky URN"
},
{
"url": "http://example.com/talk",
"mime_type": "audio",
"duration_in_seconds": 72
},
{
"title": "Invalid URL",
"url": "http://[.com/bogus"
},
{
"title": "No URL"
}
]
}]
},
"output": {
"format": "json",
"version": "1",
"entries": [
{
"id": "1",
"enclosures": [
{
"type": "image/svg+xml",
"url": "http://example.com/image",
"title": "Logo",
"size": 2345
},
{
"type": "image/png",
"url": "http://example.com/graphic.png"
},
{
"type": "image/png",
"url": "http://example.com/graphic.PNG"
},
{
"url": "data:text/plain,Hello%20World!",
"title": "Example text"
},
{
"url": "data:text/plain,File:example.jpg",
"title": "Sneaky URN"
},
{
"url": "http://example.com/talk",
"type": "audio",
"duration": 72
}
]
}
]
}
},
{
"description": "Entry image and attachments",
"input": {
"version": "https://jsonfeed.org/version/1",
"items": [{
"id": "1",
"attachments": [
{
"url": "http://example.com/image",
"mime_type": "image/svg+xml; charset=\"urf-8\"",
"title": "Logo",
"size_in_bytes": 2345
}
],
"image": "http://example.com/image"
}]
},
"output": {
"format": "json",
"version": "1",
"entries": [
{
"id": "1",
"enclosures": [
{
"type": "image",
"url": "http://example.com/image",
"preferred": true
},
{
"type": "image/svg+xml",
"url": "http://example.com/image",
"title": "Logo",
"size": 2345
}
]
}
]
}
}
]
Loading…
Cancel
Save