99use Statamic \View \Antlers \Language \Utilities \StringUtilities ;
1010use Tests \Antlers \ParserTestCase ;
1111use Tests \Factories \EntryFactory ;
12+ use Tests \FakesViews ;
1213use Tests \PreventSavingStacheItemsToDisk ;
1314
1415class PhpEnabledTest extends ParserTestCase
1516{
16- use PreventSavingStacheItemsToDisk;
17+ use FakesViews,
18+ PreventSavingStacheItemsToDisk;
1719
1820 public function test_php_has_access_to_scope_data ()
1921 {
@@ -513,8 +515,8 @@ public function test_php_node_assignments_within_loops()
513515 public function test_assignments_from_php_nodes ()
514516 {
515517 $ template = <<<'EOT'
516- {{?
517- $value_one = 100;
518+ {{?
519+ $value_one = 100;
518520 $value_two = 0;
519521?}}
520522
@@ -533,4 +535,102 @@ public function test_assignments_from_php_nodes()
533535 $ this ->assertStringContainsString ('<value_one: 1125> ' , $ result );
534536 $ this ->assertStringContainsString ('<value_two: 1025> ' , $ result );
535537 }
538+
539+ public function test_updating_variables_within_scope_using_php ()
540+ {
541+ $ data = [
542+ 'blocks ' => [
543+ [
544+ 'type ' => 'the_block ' ,
545+ ],
546+ ],
547+ ];
548+
549+ $ outerPartial = <<<'EOT'
550+ Outer Partial Before: {{ view.blocks }}{{ type }}{{ /view.blocks }}
551+ {{ partial:inner_partial :blocks="blocks" /}}
552+ Outer Partial After: {{ view.blocks }}{{ type }}{{ /view.blocks }}
553+ EOT;
554+
555+ $ innerPartial = <<<'EOT'
556+ Inner Partial Before: {{ view.blocks }}{{ type }} {{ /view.blocks }}
557+
558+ {{ if view.blocks.0 && view.blocks.0.type != 'hero_block' }}
559+ {{?
560+ array_unshift($view['blocks'], [
561+ 'type' => 'hero_block',
562+ 'simple_bard_field' => [
563+ 'type' => 'text',
564+ 'text' => 'The Text',
565+ ],
566+ ]);
567+ ?}}
568+ {{ /if }}
569+
570+ Inner Partial After: {{ view.blocks }}{{ type }} {{ /view.blocks }}
571+ EOT;
572+
573+ $ this ->withFakeViews ();
574+ $ this ->viewShouldReturnRaw ('outer_partial ' , $ outerPartial );
575+ $ this ->viewShouldReturnRaw ('inner_partial ' , $ innerPartial );
576+
577+ $ expected = <<<'EXPECTED'
578+ Outer Partial Before: the_block
579+ Inner Partial Before: the_block
580+
581+
582+
583+
584+ Inner Partial After: hero_block the_block
585+ Outer Partial After: the_block
586+ EXPECTED;
587+
588+ $ this ->assertSame (
589+ (string ) str ($ expected )->squish (),
590+ (string ) str ($ this ->renderString ('{{ partial:outer_partial :blocks="blocks" /}} ' , $ data ))->squish (),
591+ );
592+ }
593+
594+ public function test_variables_created_inside_php_do_not_override_injected_values ()
595+ {
596+ $ this ->withFakeViews ();
597+
598+ $ partial = <<<'EOT'
599+ {{? $title = 'The Title'; ?}}
600+
601+ Partial: {{ title }}
602+ EOT;
603+
604+ $ this ->viewShouldReturnRaw ('the_partial ' , $ partial );
605+
606+ $ template = <<<'EOT'
607+ Before: {{ title }}
608+ {{ partial:the_partial /}}
609+ After: {{ title }}
610+ EOT;
611+
612+ $ expected = <<<'EXPECTED'
613+ Before: The Original Title
614+
615+
616+ Partial: The Title
617+ After: The Original Title
618+ EXPECTED;
619+
620+ $ this ->assertSame (
621+ $ expected ,
622+ $ this ->renderString ($ template , ['title ' => 'The Original Title ' ]),
623+ );
624+
625+ $ template = <<<'EOT'
626+ Before: {{ title }}
627+ {{ partial:the_partial :title="title" /}}
628+ After: {{ title }}
629+ EOT;
630+
631+ $ this ->assertSame (
632+ $ expected ,
633+ $ this ->renderString ($ template , ['title ' => 'The Original Title ' ]),
634+ );
635+ }
536636}
0 commit comments