Browse Source

The plugin alias should not be ignored

This allows unexpected scenarios such as:

    $wrongPluginModel = TableRegistry::get('This.Foo');
AD7six 11 years ago
parent
commit
b86b7e13ed
2 changed files with 19 additions and 29 deletions
  1. 4 15
      src/ORM/TableRegistry.php
  2. 15 14
      tests/TestCase/ORM/TableRegistryTest.php

+ 4 - 15
src/ORM/TableRegistry.php

@@ -101,7 +101,6 @@ class TableRegistry
      */
     public static function config($alias = null, $options = null)
     {
-        list(, $alias) = pluginSplit($alias);
         if ($alias === null) {
             return static::$_config;
         }
@@ -154,9 +153,9 @@ class TableRegistry
      * @return \Cake\ORM\Table
      * @throws RuntimeException When you try to configure an alias that already exists.
      */
-    public static function get($name, array $options = [])
+    public static function get($alias, array $options = [])
     {
-        list(, $alias) = pluginSplit($name);
+        list(, $classAlias) = pluginSplit($alias);
         $exists = isset(static::$_instances[$alias]);
 
         if ($exists && !empty($options)) {
@@ -171,10 +170,10 @@ class TableRegistry
             return static::$_instances[$alias];
         }
         static::$_options[$alias] = $options;
-        $options = ['alias' => $alias] + $options;
+        $options = ['alias' => $classAlias] + $options;
 
         if (empty($options['className'])) {
-            $options['className'] = Inflector::camelize($name);
+            $options['className'] = Inflector::camelize($alias);
         }
         $className = App::className($options['className'], 'Model/Table', 'Table');
         $options['className'] = $className ?: 'Cake\ORM\Table';
@@ -199,15 +198,11 @@ class TableRegistry
     /**
      * Check to see if an instance exists in the registry.
      *
-     * Plugin names will be trimmed off of aliases as instances
-     * stored in the registry will be without the plugin name as well.
-     *
      * @param string $alias The alias to check for.
      * @return bool
      */
     public static function exists($alias)
     {
-        list(, $alias) = pluginSplit($alias);
         return isset(static::$_instances[$alias]);
     }
 
@@ -220,7 +215,6 @@ class TableRegistry
      */
     public static function set($alias, Table $object)
     {
-        list(, $alias) = pluginSplit($alias);
         return static::$_instances[$alias] = $object;
     }
 
@@ -252,16 +246,11 @@ class TableRegistry
     /**
      * Removes an instance from the registry.
      *
-     * Plugin name will be trimmed off of aliases as instances
-     * stored in the registry will be without the plugin name as well.
-     *
      * @param string $alias The alias to remove.
      * @return void
      */
     public static function remove($alias)
     {
-        list(, $alias) = pluginSplit($alias);
-
         unset(
             static::$_instances[$alias],
             static::$_config[$alias],

+ 15 - 14
tests/TestCase/ORM/TableRegistryTest.php

@@ -102,10 +102,6 @@ class TableRegistryTest extends TestCase
 
         $result = TableRegistry::config('TestPlugin.TestPluginComments', $data);
         $this->assertEquals($data, $result, 'Returns config data.');
-
-        $result = TableRegistry::config();
-        $expected = ['TestPluginComments' => $data];
-        $this->assertEquals($expected, $result);
     }
 
     /**
@@ -226,9 +222,9 @@ class TableRegistryTest extends TestCase
 
         $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
         $this->assertInstanceOf($class, $table);
-        $this->assertTrue(
+        $this->assertFalse(
             TableRegistry::exists('TestPluginComments'),
-            'Short form should exist'
+            'Short form should NOT exist'
         );
         $this->assertTrue(
             TableRegistry::exists('TestPlugin.TestPluginComments'),
@@ -237,9 +233,6 @@ class TableRegistryTest extends TestCase
 
         $second = TableRegistry::get('TestPlugin.TestPluginComments');
         $this->assertSame($table, $second, 'Can fetch long form');
-
-        $second = TableRegistry::get('TestPluginComments');
-        $this->assertSame($table, $second);
     }
 
     /**
@@ -389,7 +382,6 @@ class TableRegistryTest extends TestCase
 
         $this->assertSame($mock, TableRegistry::set('TestPlugin.Comments', $mock));
         $this->assertSame($mock, TableRegistry::get('TestPlugin.Comments'));
-        $this->assertSame($mock, TableRegistry::get('Comments'));
     }
 
     /**
@@ -418,14 +410,23 @@ class TableRegistryTest extends TestCase
         $pluginTable = TableRegistry::get('TestPlugin.Comments');
         $cachedTable = TableRegistry::get('Comments');
 
+        $this->assertTrue(TableRegistry::exists('TestPlugin.Comments'));
+        $this->assertTrue(TableRegistry::exists('Comments'));
+        $this->assertNotSame($pluginTable, $cachedTable);
+
+        TableRegistry::remove('TestPlugin.Comments');
+        $this->assertFalse(TableRegistry::exists('TestPlugin.Comments'));
         $this->assertTrue(TableRegistry::exists('Comments'));
-        $this->assertSame($pluginTable, $cachedTable);
 
         TableRegistry::remove('Comments');
+        $this->assertFalse(TableRegistry::exists('TestPlugin.Comments'));
         $this->assertFalse(TableRegistry::exists('Comments'));
 
-        $appTable = TableRegistry::get('Comments');
-        $this->assertTrue(TableRegistry::exists('Comments'));
-        $this->assertNotSame($pluginTable, $appTable);
+        $pluginTable = TableRegistry::get('TestPlugin.Comments');
+        $cachedTable = TableRegistry::get('Comments');
+
+        TableRegistry::remove('Comments');
+        $this->assertTrue(TableRegistry::exists('TestPlugin.Comments'));
+        $this->assertFalse(TableRegistry::exists('Comments'));
     }
 }