From 24ea2638fe68a764a150fb04a819d62b17cd2c49 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Mon, 12 Feb 2018 11:05:38 +0000 Subject: [PATCH 1/2] Create licence file so that GitHub (and humans) can more easily find it --- LICENSE | 29 +++++++++++++++++++++++++++++ README.md | 20 -------------------- 2 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 000000000..65f4b0c70 --- /dev/null +++ b/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2006-2018, SilverStripe Limited - www.silverstripe.com +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md index dc579b3b9..8f8c6b34a 100644 --- a/README.md +++ b/README.md @@ -34,26 +34,6 @@ If you would like to make changes to the SilverStripe core codebase, we have an * [Bugtracker: Installer](https://github.com/silverstripe/silverstripe-installer/issues) * [Forums](http://silverstripe.org/forums) * [Developer Mailinglist](https://groups.google.com/forum/#!forum/silverstripe-dev) - -## License ## - - Copyright (c) 2006-2014, SilverStripe Limited - www.silverstripe.com - All rights reserved. - - Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - * Neither the name of SilverStripe nor the names of its contributors may be used to endorse or promote products derived from this software - without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - OF SUCH DAMAGE. ## Attribution ## From c767e472dc494408460ef47c27b8d34475da4ac6 Mon Sep 17 00:00:00 2001 From: Jonathon Menz Date: Mon, 12 Feb 2018 20:29:48 -0800 Subject: [PATCH 2/2] FIX DataObject singleton creation Ensure DataObject instances are aware they are singletons so functions like populateDefaults() can be skipped. (fixes #4878) --- control/injector/Injector.php | 5 ++++ tests/model/DataObjectTest.php | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/control/injector/Injector.php b/control/injector/Injector.php index 1c247b35e..96c69854b 100644 --- a/control/injector/Injector.php +++ b/control/injector/Injector.php @@ -551,6 +551,11 @@ class Injector { $constructorParams = $spec['constructor']; } + // If we're dealing with a DataObject, pass through Singleton flag as second argument + if ($type != 'prototype' && empty($constructorParams) && is_subclass_of($class, 'DataObject')) { + $constructorParams = array(null, true); + } + $factory = isset($spec['factory']) ? $this->get($spec['factory']) : $this->getObjectCreator(); $object = $factory->create($class, $constructorParams); diff --git a/tests/model/DataObjectTest.php b/tests/model/DataObjectTest.php index 8d7cdf474..8855693eb 100644 --- a/tests/model/DataObjectTest.php +++ b/tests/model/DataObjectTest.php @@ -33,6 +33,51 @@ class DataObjectTest extends SapphireTest { 'ManyManyListTest_Category', ); + /** + * @dataProvider provideSingletons + */ + public function testSingleton($inst, $defaultValue, $altDefaultValue) + { + $inst = $inst(); + // Test that populateDefaults() isn't called on singletons + // which can lead to SQL errors during build, and endless loops + if ($defaultValue) { + $this->assertEquals($defaultValue, $inst->MyFieldWithDefault); + } else { + $this->assertEmpty($inst->MyFieldWithDefault); + } + + if ($altDefaultValue) { + $this->assertEquals($altDefaultValue, $inst->MyFieldWithAltDefault); + } else { + $this->assertEmpty($inst->MyFieldWithAltDefault); + } + } + + public function provideSingletons() + { + // because PHPUnit evalutes test providers *before* setUp methods + // any extensions added in the setUp methods won't be available + // we must return closures to generate the arguments at run time + return array( + array(function () { + return DataObjectTest_Fixture::create(); + }, 'Default Value', 'Default Value'), + array(function () { + return new DataObjectTest_Fixture(); + }, 'Default Value', 'Default Value'), + array(function () { + return singleton('DataObjectTest_Fixture'); + }, null, null), + array(function () { + return DataObjectTest_Fixture::singleton(); + }, null, null), + array(function () { + return new DataObjectTest_Fixture(null, true); + }, null, null), + ); + } + public function testDb() { $obj = new DataObjectTest_TeamComment(); $dbFields = $obj->db();