Browse Source

FixtureManager should use connection aliases

If someone was to implement the FixtureInterface and forget a `test_`
prefix in their connection method their live data should not be wiped
by the test harness. Instead, the FixtureManager should use the
generated aliases which should point at safe connections.

Merge branch 'issue-10073' into master
Mark Story 9 years ago
parent
commit
67993de66b

+ 1 - 1
src/TestSuite/Fixture/FixtureManager.php

@@ -360,7 +360,7 @@ class FixtureManager
     {
         $dbs = $this->_fixtureConnections($fixtures);
         foreach ($dbs as $connection => $fixtures) {
-            $db = ConnectionManager::get($connection, false);
+            $db = ConnectionManager::get($connection);
             $logQueries = $db->logQueries();
             if ($logQueries && !$this->_debug) {
                 $db->logQueries(false);

+ 62 - 0
tests/Fixture/OtherArticlesFixture.php

@@ -0,0 +1,62 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         1.2.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\Fixture;
+
+use Cake\Datasource\ConnectionInterface;
+use Cake\Datasource\FixtureInterface;
+
+/**
+ * A fixture attached to the non-default connection
+ * that implements the interface with none of the safe-guards
+ * from TestFixture.
+ */
+class OtherArticlesFixture implements FixtureInterface
+{
+    public $table = 'other_articles';
+
+    public function create(ConnectionInterface $db)
+    {
+    }
+
+    public function drop(ConnectionInterface $db)
+    {
+    }
+
+    public function insert(ConnectionInterface $db)
+    {
+    }
+
+    public function createConstraints(ConnectionInterface $db)
+    {
+    }
+
+    public function dropConstraints(ConnectionInterface $db)
+    {
+    }
+
+    public function truncate(ConnectionInterface $db)
+    {
+    }
+
+    public function connection()
+    {
+        return 'other';
+    }
+
+    public function sourceName()
+    {
+        return 'other_articles';
+    }
+}

+ 49 - 0
tests/TestCase/TestSuite/FixtureManagerTest.php

@@ -255,6 +255,55 @@ class FixtureManagerTest extends TestCase
     }
 
     /**
+     * Test load uses aliased connections via a mock.
+     *
+     * Ensure that FixtureManager uses connection aliases
+     * protecting 'live' tables from being wiped by mistakes in
+     * fixture connection names.
+     *
+     * @return void
+     */
+    public function testLoadConnectionAliasUsage()
+    {
+        $connection = ConnectionManager::get('test');
+        $statement = $this->getMockBuilder('Cake\Database\StatementInterface')
+            ->getMock();
+
+        // This connection should _not_ be used.
+        $other = $this->getMockBuilder('Cake\Database\Connection')
+            ->setMethods(['execute'])
+            ->setConstructorArgs([['driver' => $connection->getDriver()]])
+            ->getMock();
+        $other->expects($this->never())
+            ->method('execute')
+            ->will($this->returnValue($statement));
+
+        // This connection should be used instead of
+        // the 'other' connection as the alias should not be ignored.
+        $testOther = $this->getMockBuilder('Cake\Database\Connection')
+            ->setMethods(['execute'])
+            ->setConstructorArgs([['driver' => $connection->getDriver()]])
+            ->getMock();
+        $testOther->expects($this->atLeastOnce())
+            ->method('execute')
+            ->will($this->returnValue($statement));
+
+        ConnectionManager::config('other', $other);
+        ConnectionManager::config('test_other', $testOther);
+
+        // Connect the alias making test_other an alias of other.
+        ConnectionManager::alias('test_other', 'other');
+
+        $test = $this->getMockBuilder('Cake\TestSuite\TestCase')->getMock();
+        $test->fixtures = ['core.other_articles'];
+        $this->manager->fixturize($test);
+        $this->manager->load($test);
+
+        ConnectionManager::drop('other');
+        ConnectionManager::drop('test_other');
+    }
+
+    /**
      * Test loading fixtures using loadSingle()
      *
      * @return void