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

Fix data being passed into recursive vars #2719

Merged
merged 1 commit into from
Oct 22, 2020
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
2 changes: 1 addition & 1 deletion src/Tags/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ 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($this->context->all(), $data, [
return array_merge($data, [
'children' => $children,
'parent' => $parent,
'depth' => $depth,
Expand Down
2 changes: 1 addition & 1 deletion src/View/Antlers/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ public function parseRecursives($text, $orig_text, $data)
$has_children = false;
}

$replacement = $this->parse($orig_text, $child);
$replacement = $this->parse($orig_text, array_merge($data, $child));

// If this is the first loop we'll use $tag as reference, if not
// we'll use the previous tag ($next_tag)
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/Addon/Tags/RecursiveChildren.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function index()
'children' => [
[
'title' => 'Four',
'foo' => 'Baz',
],
],
],
Expand Down
100 changes: 88 additions & 12 deletions tests/Tags/StructureTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Tests\Tags;

use Facades\Tests\Factories\EntryFactory;
use Statamic\Facades\Antlers;
use Statamic\Facades\Entry;
use Statamic\Facades\Nav;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
Expand All @@ -14,16 +16,7 @@ class StructureTagTest extends TestCase
/** @test */
public function it_renders()
{
$this->makeNav([
['title' => 'One', 'children' => [
['title' => 'One One'],
]],
['title' => 'Two'],
['title' => 'Three', 'children' => [
['title' => 'Three One'],
['title' => 'Three Two'],
]],
]);
$this->createNav();

// The html uses <i> tags (could be any tag, but i is short) to prevent whitespace comparison issues in the assertion.
$template = <<<'EOT'
Expand Down Expand Up @@ -52,7 +45,7 @@ public function it_renders()
</ul>
</li>
<li>
<i>Two bar</i>
<i>Two notbar</i>
</li>
<li>
<i>Three bar</i>
Expand All @@ -61,7 +54,7 @@ public function it_renders()
<i>Three One bar</i>
</li>
<li>
<i>Three Two bar</i>
<i>Three Two notbar</i>
</li>
</ul>
</li>
Expand All @@ -74,6 +67,61 @@ public function it_renders()
]));
}

/** @test */
public function it_renders_with_scope()
{
$this->createNav();

// 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 scope="n" }}
<li>
<i>{{ n:nav_title or title }} {{ foo }}</i>
{{ if children }}
<ul>
{{ *recursive n:children* }}
</ul>
{{ /if }}
</li>
{{ /nav:test }}
</ul>
EOT;

$expected = <<<'EOT'
<ul>
<li>
<i>Navtitle One bar</i>
<ul>
<li>
<i>Navtitle One One bar</i>
</li>
</ul>
</li>
<li>
<i>Two notbar</i>
</li>
<li>
<i>Three bar</i>
<ul>
<li>
<i>Navtitle Three One bar</i>
</li>
<li>
<i>Three Two notbar</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.
'nav_title' => 'outer nav_title', // to test that the cascade doesn't leak into the iterated scope
]));
}

private function makeNav($tree)
{
$nav = Nav::make('test');
Expand All @@ -82,4 +130,32 @@ private function makeNav($tree)

$nav->save();
}

private function createNav()
{
$one = EntryFactory::collection('pages')->data(['title' => 'One', 'nav_title' => 'Navtitle One'])->create();
$oneOne = EntryFactory::collection('pages')->data(['title' => 'One One', 'nav_title' => 'Navtitle One One'])->create();
$two = EntryFactory::collection('pages')->data(['title' => 'Two', 'foo' => 'notbar'])->create();
$three = EntryFactory::collection('pages')->data(['title' => 'Three'])->create();
$threeOne = EntryFactory::collection('pages')->data(['title' => 'Three One', 'nav_title' => 'Navtitle Three One'])->create();
$threeTwo = EntryFactory::collection('pages')->data(['title' => 'Three Two', 'foo' => 'notbar'])->create();

Entry::shouldReceive('find')->with('1')->andReturn($one);
Entry::shouldReceive('find')->with('1-1')->andReturn($oneOne);
Entry::shouldReceive('find')->with('2')->andReturn($two);
Entry::shouldReceive('find')->with('3')->andReturn($three);
Entry::shouldReceive('find')->with('3-1')->andReturn($threeOne);
Entry::shouldReceive('find')->with('3-2')->andReturn($threeTwo);

$this->makeNav([
['entry' => '1', 'children' => [
['entry' => '1-1'],
]],
['entry' => '2'],
['entry' => '3', 'children' => [
['entry' => '3-1'],
['entry' => '3-2'],
]],
]);
}
}
12 changes: 6 additions & 6 deletions tests/View/Antlers/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,23 +575,23 @@ public function testRecursiveChildren()
// the variables are inside RecursiveChildren@index
$this->app['statamic.tags']['recursive_children'] = \Tests\Fixtures\Addon\Tags\RecursiveChildren::class;

$template = '<ul>{{ recursive_children }}<li>{{ title }}{{ if children }}<ul>{{ *recursive children* }}</ul>{{ /if }}</li>{{ /recursive_children }}</ul>';
$template = '<ul>{{ recursive_children }}<li>{{ title }}.{{ foo }}{{ if children }}<ul>{{ *recursive children* }}</ul>{{ /if }}</li>{{ /recursive_children }}</ul>';

$expected = '<ul><li>One<ul><li>Two</li><li>Three<ul><li>Four</li></ul></li></ul></li></ul>';
$expected = '<ul><li>One.Bar<ul><li>Two.Bar</li><li>Three.Bar<ul><li>Four.Baz</li></ul></li></ul></li></ul>';

$this->assertEquals($expected, $this->parse($template, []));
$this->assertEquals($expected, $this->parse($template, ['foo' => 'Bar']));
}

public function testRecursiveChildrenWithScope()
{
// the variables are inside RecursiveChildren@index
$this->app['statamic.tags']['recursive_children'] = \Tests\Fixtures\Addon\Tags\RecursiveChildren::class;

$template = '<ul>{{ recursive_children scope="item" }}<li>{{ item:title }}{{ if item:children }}<ul>{{ *recursive item:children* }}</ul>{{ /if }}</li>{{ /recursive_children }}</ul>';
$template = '<ul>{{ recursive_children scope="item" }}<li>{{ item:title }}.{{ item:foo }}.{{ foo }}{{ if item:children }}<ul>{{ *recursive item:children* }}</ul>{{ /if }}</li>{{ /recursive_children }}</ul>';

$expected = '<ul><li>One<ul><li>Two</li><li>Three<ul><li>Four</li></ul></li></ul></li></ul>';
$expected = '<ul><li>One..Bar<ul><li>Two..Bar</li><li>Three..Bar<ul><li>Four.Baz.Baz</li></ul></li></ul></li></ul>';

$this->assertEquals($expected, $this->parse($template, []));
$this->assertEquals($expected, $this->parse($template, ['foo' => 'Bar']));
}

public function testEmptyValuesAreNotOverriddenByPreviousIteration()
Expand Down