Skip to content

Commit

Permalink
Prevent context stomping over the page data, with tests. #2610
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga committed Oct 8, 2020
1 parent 2b88b22 commit a8f3f47
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Tags/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public function toArray($tree, $parent = null, $depth = 1)
$data = $page->toAugmentedArray();
$children = empty($item['children']) ? [] : $this->toArray($item['children'], $data, $depth + 1);

return array_merge($data, [
return array_merge($this->context->all(), $data, [
'children' => $children,
'parent' => $parent,
'depth' => $depth,
'is_current' => rtrim(URL::getCurrent(), '/') == rtrim($page->url(), '/'),
'is_parent' => Site::current()->url() === $page->url() ? false : URL::isAncestorOf(URL::getCurrent(), $page->url()),
'is_external' => URL::isExternal($page->absoluteUrl()),
], $this->context->all());
]);
})->filter()->values()->all();
}
}
85 changes: 85 additions & 0 deletions tests/Tags/StructureTagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Tests\Tags;

use Statamic\Facades\Antlers;
use Statamic\Facades\Nav;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;

class StructureTagTest extends TestCase
{
use PreventSavingStacheItemsToDisk;

/** @test */
public function it_renders()
{
$this->makeNav([
['title' => 'One', 'children' => [
['title' => 'One One'],
]],
['title' => 'Two'],
['title' => 'Three', 'children' => [
['title' => 'Three One'],
['title' => 'Three Two'],
]],
]);

// The html uses <i> tags (could be any tag, but i is short) to prevent whitespace comparison issues in the assertion.
$template = <<<'EOT'
<ul>
{{ nav:test }}
<li>
<i>{{ title }} {{ foo }}</i>
{{ if children }}
<ul>
{{ *recursive children* }}
</ul>
{{ /if }}
</li>
{{ /nav:test }}
</ul>
EOT;

$expected = <<<'EOT'
<ul>
<li>
<i>One bar</i>
<ul>
<li>
<i>One One bar</i>
</li>
</ul>
</li>
<li>
<i>Two bar</i>
</li>
<li>
<i>Three bar</i>
<ul>
<li>
<i>Three One bar</i>
</li>
<li>
<i>Three Two bar</i>
</li>
</ul>
</li>
</ul>
EOT;

$this->assertXmlStringEqualsXmlString($expected, (string) Antlers::parse($template, [
'foo' => 'bar', // to test that cascade is inherited.
'title' => 'outer title', // to test that cascade the page's data takes precedence over the cascading data.
]));
}

private function makeNav($tree)
{
$nav = Nav::make('test');

$nav->addTree($nav->makeTree('en')->tree($tree));

$nav->save();
}
}

0 comments on commit a8f3f47

Please sign in to comment.