Skip to content

Commit

Permalink
Rename the test methods and revert the security.helper changes
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Oct 23, 2019
1 parent 485faf2 commit 1b0269f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 63 deletions.
14 changes: 7 additions & 7 deletions core-bundle/src/Resources/contao/pages/PageRegular.php
Original file line number Diff line number Diff line change
Expand Up @@ -816,9 +816,9 @@ protected function createFooterScripts($objLayout, $objPage = null)
{
$noSearch = (bool) $objPage->noSearch;

// Backwards compatibility: Do not use $GLOBALS['TL_NOINDEX_KEYS'] anymore
// but make sure your page type delivers the correct meta robots tag
if (preg_grep('/^(' . implode('|', $GLOBALS['TL_NOINDEX_KEYS']) . ')$/', array_keys($_GET))) {
// Do not search the page if the query has a key that is in TL_NOINDEX_KEYS
if (preg_grep('/^(' . implode('|', $GLOBALS['TL_NOINDEX_KEYS']) . ')$/', array_keys($_GET)))
{
$noSearch = true;
}

Expand All @@ -835,14 +835,14 @@ protected function createFooterScripts($objLayout, $objPage = null)
'fePreview' => System::getContainer()->get('contao.security.token_checker')->isPreviewMode()
);

$user = System::getContainer()->get('security.helper')->getUser();
$token = System::getContainer()->get('security.token_storage')->getToken();

if ($user instanceof FrontendUser)
if ($token !== null && $token->getUser() instanceof FrontendUser)
{
$meta['memberId'] = (int) $user->id;
$meta['memberId'] = (int) $token->getUser()->id;
}

$strScripts .= "\n<script type=\"application/ld+json\">" . json_encode($meta) . "</script>\n";
$strScripts .= '<script type="application/ld+json">' . json_encode($meta) . '</script>';
}

// Add the custom JavaScript
Expand Down
79 changes: 23 additions & 56 deletions core-bundle/tests/Search/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,7 @@

class DocumentTest extends TestCase
{
/**
* @dataProvider documentProvider
*/
public function testDocument(string $body, array $expectedJsonLds): void
{
$document = new Document(
new Uri('https://example.com'),
200,
['Content-Type' => ['text/html']],
$body
);

$this->assertSame('https://example.com', (string) $document->getUri());
$this->assertSame(200, $document->getStatusCode());
$this->assertSame(['content-type' => ['text/html']], $document->getHeaders());
$this->assertSame($expectedJsonLds, $document->extractJsonLdScripts());
}

public function testFactory(): void
public function testCreatesADocumentFromTheRequestAndResponse(): void
{
$request = Request::create('https://example.com/foo?bar=baz', 'GET');
$response = new Response('body', 200, ['content-type' => ['text/html']]);
Expand All @@ -53,50 +35,22 @@ public function testFactory(): void
$this->assertSame(['content-type' => ['text/html'], 'cache-control' => ['no-cache, private']], $headers);
}

public function testContextAndTypeFiltersCorrectly(): void
/**
* @dataProvider documentProvider
*/
public function testExtractsTheJsdonLdScript(string $body, array $expectedJsonLds): void
{
$document = new Document(
new Uri('https://example.com'),
200,
['Content-Type' => ['text/html']],
'<html><body><script type="application/ld+json">{"@context":"https:\/\/contao.org\/","@type":"PageMetaData","foobar":true}</script></body></html>'
);

$this->assertSame(
[
[
'@context' => 'https://contao.org/',
'@type' => 'PageMetaData',
'foobar' => true,
],
],
$document->extractJsonLdScripts()
);

$this->assertSame(
[
[
'@context' => 'https://contao.org/',
'@type' => 'PageMetaData',
'foobar' => true,
],
],
$document->extractJsonLdScripts('https://contao.org/')
);

$this->assertSame(
[
[
'@context' => 'https://contao.org/',
'@type' => 'PageMetaData',
'foobar' => true,
],
],
$document->extractJsonLdScripts('https://contao.org/', 'PageMetaData')
$body
);

$this->assertSame([], $document->extractJsonLdScripts('https://example.com/'));
$this->assertSame([], $document->extractJsonLdScripts('https://contao.org/', 'nonsense-type'));
$this->assertSame('https://example.com', (string) $document->getUri());
$this->assertSame(200, $document->getStatusCode());
$this->assertSame(['content-type' => ['text/html']], $document->getHeaders());
$this->assertSame($expectedJsonLds, $document->extractJsonLdScripts());
}

public function documentProvider(): \Generator
Expand Down Expand Up @@ -144,4 +98,17 @@ public function documentProvider(): \Generator
],
];
}

public function testDoesNotExtractTheJsdonLdScriptIfContextOrTypeDoNotMatch(): void
{
$document = new Document(
new Uri('https://example.com'),
200,
['Content-Type' => ['text/html']],
'<html><body><script type="application/ld+json">{"@context":"https:\/\/contao.org\/","@type":"PageMetaData","foobar":true}</script></body></html>'
);

$this->assertSame([], $document->extractJsonLdScripts('https://example.com/'));
$this->assertSame([], $document->extractJsonLdScripts('https://contao.org/', 'nonsense-type'));
}
}

0 comments on commit 1b0269f

Please sign in to comment.