diff --git a/src/Tags/Structure.php b/src/Tags/Structure.php index d08160800d..c3b4ab01b5 100644 --- a/src/Tags/Structure.php +++ b/src/Tags/Structure.php @@ -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, diff --git a/src/View/Antlers/Parser.php b/src/View/Antlers/Parser.php index 961a3add5b..669dc38245 100644 --- a/src/View/Antlers/Parser.php +++ b/src/View/Antlers/Parser.php @@ -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) diff --git a/tests/Fixtures/Addon/Tags/RecursiveChildren.php b/tests/Fixtures/Addon/Tags/RecursiveChildren.php index 635c604cce..88f3c50103 100644 --- a/tests/Fixtures/Addon/Tags/RecursiveChildren.php +++ b/tests/Fixtures/Addon/Tags/RecursiveChildren.php @@ -18,6 +18,7 @@ public function index() 'children' => [ [ 'title' => 'Four', + 'foo' => 'Baz', ], ], ], diff --git a/tests/Tags/StructureTagTest.php b/tests/Tags/StructureTagTest.php index bdacb8cab9..495b1d2fc2 100644 --- a/tests/Tags/StructureTagTest.php +++ b/tests/Tags/StructureTagTest.php @@ -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; @@ -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 tags (could be any tag, but i is short) to prevent whitespace comparison issues in the assertion. $template = <<<'EOT' @@ -52,7 +45,7 @@ public function it_renders()
  • - Two bar + Two notbar
  • Three bar @@ -61,7 +54,7 @@ public function it_renders() Three One bar
  • - Three Two bar + Three Two notbar
  • @@ -74,6 +67,61 @@ public function it_renders() ])); } + /** @test */ + public function it_renders_with_scope() + { + $this->createNav(); + + // The html uses tags (could be any tag, but i is short) to prevent whitespace comparison issues in the assertion. + $template = <<<'EOT' + +EOT; + + $expected = <<<'EOT' + +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'); @@ -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'], + ]], + ]); + } } diff --git a/tests/View/Antlers/ParserTest.php b/tests/View/Antlers/ParserTest.php index 3057837c3d..1df652eb66 100644 --- a/tests/View/Antlers/ParserTest.php +++ b/tests/View/Antlers/ParserTest.php @@ -575,11 +575,11 @@ public function testRecursiveChildren() // the variables are inside RecursiveChildren@index $this->app['statamic.tags']['recursive_children'] = \Tests\Fixtures\Addon\Tags\RecursiveChildren::class; - $template = ''; + $template = ''; - $expected = ''; + $expected = ''; - $this->assertEquals($expected, $this->parse($template, [])); + $this->assertEquals($expected, $this->parse($template, ['foo' => 'Bar'])); } public function testRecursiveChildrenWithScope() @@ -587,11 +587,11 @@ public function testRecursiveChildrenWithScope() // the variables are inside RecursiveChildren@index $this->app['statamic.tags']['recursive_children'] = \Tests\Fixtures\Addon\Tags\RecursiveChildren::class; - $template = ''; + $template = ''; - $expected = ''; + $expected = ''; - $this->assertEquals($expected, $this->parse($template, [])); + $this->assertEquals($expected, $this->parse($template, ['foo' => 'Bar'])); } public function testEmptyValuesAreNotOverriddenByPreviousIteration()