Browse Source

Ensure deprecated properties like $theme are properly passed to view.

Refs #7012
ADmad 10 years ago
parent
commit
d6066af471
2 changed files with 39 additions and 1 deletions
  1. 17 1
      src/View/ViewVarsTrait.php
  2. 22 0
      tests/TestCase/Controller/ControllerTest.php

+ 17 - 1
src/View/ViewVarsTrait.php

@@ -110,12 +110,28 @@ trait ViewVarsTrait
             throw new Exception\MissingViewException([$viewClass]);
         }
 
+        $validViewOptions = $this->viewOptions();
         $viewOptions = [];
-        foreach ($this->viewOptions() as $option) {
+        foreach ($validViewOptions as $option) {
             if (property_exists($this, $option)) {
                 $viewOptions[$option] = $this->{$option};
             }
         }
+        $deprecatedOptions = array_diff(
+            ['layout', 'view', 'theme', 'autoLayout', 'viewPath', 'layoutPath'],
+            $validViewOptions
+        );
+        foreach ($deprecatedOptions as $option) {
+            if (property_exists($this, $option)) {
+                $viewOptions[$option] = $this->{$option};
+                unset($this->{$option});
+                trigger_error(sprintf(
+                    'Property $%s is deprecated. Use $this->getView()->%s() instead in beforeRender().',
+                    $option,
+                    $option
+                ), E_USER_DEPRECATED);
+            }
+        }
         return new $className($this->request, $this->response, $this->eventManager(), $viewOptions);
     }
 

+ 22 - 0
tests/TestCase/Controller/ControllerTest.php

@@ -66,6 +66,13 @@ class TestController extends ControllerTestAppController
 {
 
     /**
+     * Theme property
+     *
+     * @var string
+     */
+    public $theme = 'Foo';
+
+    /**
      * helpers property
      *
      * @var array
@@ -947,4 +954,19 @@ class ControllerTest extends TestCase
         $this->assertFalse($controller->isAction('beforeFilter'));
         $this->assertTrue($controller->isAction('index'));
     }
+
+    /**
+     * Test declared deprecated properties like $theme are properly passed to view.
+     * @return void
+     */
+    public function testDeclaredDeprecatedProperty()
+    {
+        $controller = new TestController(new Request(), new Response());
+        $theme = $controller->theme;
+
+        // @codingStandardsIgnoreStart
+        $this->assertEquals($theme, @$controller->getView()->theme);
+        // @codingStandardsIgnoreEnd
+    }
+
 }