Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/structuredtext linkresolver #19

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/Prismic/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,7 @@ public function getHtml($field, $linkResolver = null)
{
$fragment = $this->get($field);
if (isset($fragment) && method_exists($fragment, 'asHtml')) {
if ($fragment instanceof DocumentLink) {
return $fragment->asHtml($linkResolver);
} else {
return $fragment->asHtml();
}
return $fragment->asHtml($linkResolver);
}

return "";
Expand Down
2 changes: 1 addition & 1 deletion src/Prismic/Fragment/Link/DocumentLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct($id, $type, $tags, $slug, $isBroken)

public function asHtml($linkResolver)
{
return '<a href="' . $linkResolver->resolve($this) . '">' . $this->slug . '</a>';
return '<a href="' . $linkResolver($this) . '">' . $this->slug . '</a>';
}

public static function parse($json)
Expand Down
5 changes: 5 additions & 0 deletions src/Prismic/LinkResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ abstract class LinkResolver
*/
abstract public function resolve($link);

public function __invoke($link)
{
return $this->resolve($link);
}

/**
* Returns the application-specific URL related to this Document
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Prismic/FakeLinkResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Prismic\Test;

use Prismic\LinkResolver;

class FakeLinkResolver extends LinkResolver
{
public function resolve($link)
{
return "http://host/doc/".$link->getId();
}
}
16 changes: 7 additions & 9 deletions tests/Prismic/LinkResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
use Prismic\API;
use Prismic\Document;
use Prismic\Fragment\Link\DocumentLink;
use Prismic\LinkResolver;

class FakeLinkResolver extends LinkResolver
{
public function resolve($link)
{
return "http://host/doc/".$link->getId();
}
}

class LinkResolverTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -53,6 +44,13 @@ public function testResolve()
$this->assertEquals($content, $this->linkResolver->resolve($this->link));
}

public function testCall()
{
$content = 'http://host/doc/Ue0EDd_mqb8Dhk3j';
$linkResolver = $this->linkResolver;
$this->assertEquals($content, $linkResolver($this->link));
}

public function testResolveDocument()
{
$content = "http://host/doc/Ue0EDd_mqb8Dhk3j";
Expand Down
33 changes: 33 additions & 0 deletions tests/Prismic/StructuredTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,37 @@ public function testPreformattedBlockEscapeRendering()
$this->assertEquals($content, $structuredText->asHtml());
}

public function testDocumentLinkRendering()
{
$linkResolver = new FakeLinkResolver();
$structuredText = $this->document->getStructuredText('product.listLinks');
$content = '<p><a href="http://host/doc/UjHkUrGIJ7cBlWAb">link1</a></p>';
$this->assertEquals($content, $structuredText->asHtml($linkResolver));
}

public function testNestedDocumentLinkRendering()
{
$linkResolver = new FakeLinkResolver();
$content = '<section data-field="product.listLinks"><p>' .
'<a href="http://host/doc/UjHkUrGIJ7cBlWAb">link1</a></p></section>';
// There should be better way (.*? - (?!)) than this one but no one seems to work.
$notSection = '([^<]|<[^\/]|<\/[^s]|<\/s[^e]|<\/se[^c]|<\/sec[^t]|<\/sect[^i]|' .
'<\/secti[^o]|<\/sectio[^n]|<\/section[^>])';
$html = preg_replace(
'/.*(<section data-field="product.listLinks">' . $notSection . '*<\/section>).*/',
'$1',
$this->document->asHtml($linkResolver)
);
$this->assertEquals($content, $html);
}

public function testDocumentLinkWithLamdaRendering()
{
$linkResolver = function ($link) {
return "http://host/document/".$link->getId();
};
$structuredText = $this->document->getStructuredText('product.listLinks');
$content = '<p><a href="http://host/document/UjHkUrGIJ7cBlWAb">link1</a></p>';
$this->assertEquals($content, $structuredText->asHtml($linkResolver));
}
}
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
} elseif (file_exists($file = __DIR__.'/autoload.php.dist')) {
require_once $file;
}

require_once 'Prismic/FakeLinkResolver.php';
30 changes: 30 additions & 0 deletions tests/fixtures/search.json
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,36 @@
}
}
],
"listLinks": {
"type": "StructuredText",
"value": [
{
"type": "paragraph",
"text": "link1",
"spans": [
{
"start": 0,
"end": 5,
"type": "hyperlink",
"data": {
"type": "Link.document",
"value": {
"document": {
"id": "UjHkUrGIJ7cBlWAb",
"type": "product",
"tags": [
"Pie"
],
"slug": "apricot-pie"
},
"isBroken": false
}
}
}
]
}
]
},
"relatedImages": [
{
"type": "Image",
Expand Down