Resolves #64135: Block any write access to the static models
authorstan <stan@735d13b6-9817-0410-8766-e36946ffe9aa>
Tue, 10 Feb 2015 06:17:09 +0000 (06:17 +0000)
committerstan <stan@735d13b6-9817-0410-8766-e36946ffe9aa>
Tue, 10 Feb 2015 06:17:09 +0000 (06:17 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Extensions/static_info_tables/trunk@88351 735d13b6-9817-0410-8766-e36946ffe9aa

ChangeLog
Classes/Domain/Repository/AbstractEntityRepository.php
Tests/Unit/Domain/Repository/AbstractEntityRepositoryTest.php [new file with mode: 0644]

index fb74a73..0357c19 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2015-02-10  Oliver Klee <typo3-coding@oliverklee.de>
+
+       * Resolves #64135: Block any write access to the static models
+
 2015-02-10  Stanislas Rolland  <typo3@sjbr.ca>
 
        * Escape some backslashes in class names
index 9b31fa2..8b1dfbc 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 namespace SJBR\StaticInfoTables\Domain\Repository;
+
 /***************************************************************
 *  Copyright notice
 *
@@ -25,8 +26,14 @@ namespace SJBR\StaticInfoTables\Domain\Repository;
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/
 
+use TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface;
+
 /**
  * Abstract Repository for static entities
+ *
+ * @author Armin RĂ¼diger Vieweg <info@professorweb.de>
+ * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
+ * @author Oliver Klee <typo3-coding@oliverklee.de>
  */
 abstract class AbstractEntityRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
 
@@ -41,6 +48,11 @@ abstract class AbstractEntityRepository extends \TYPO3\CMS\Extbase\Persistence\R
        protected $dataMapper;
 
        /**
+        * @var array ISO keys for this static table
+        */
+       protected $isoKeys = array();
+
+       /**
         * Injects the DataMapper to map nodes to objects
         *
         * @param \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper
@@ -51,10 +63,17 @@ abstract class AbstractEntityRepository extends \TYPO3\CMS\Extbase\Persistence\R
        }
 
        /**
-        * @var array ISO keys for this static table
+        * Initializes the repository.
+        *
+        * @return void
         */
-       protected $isoKeys = array();
-       
+       public function initializeObject() {
+               /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings $querySettings */
+               $querySettings = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
+               $querySettings->setRespectStoragePage(FALSE);
+               $this->setDefaultQuerySettings($querySettings);
+       }
+
        /**
         * Find all with deleted included
         *
@@ -102,7 +121,7 @@ abstract class AbstractEntityRepository extends \TYPO3\CMS\Extbase\Persistence\R
         * @param \TYPO3\CMS\Extbase\Persistence\QueryResultInterface $entities to be sorted
         * @param string $orderDirection may be "asc" or "desc". Default is "asc".
         * @return array entities ordered by localized name
-        */     
+        */
        public function localizedSort(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface $entities, $orderDirection = 'asc') {
                $result = $entities->toArray();
                $locale = \SJBR\StaticInfoTables\Utility\LocalizationUtility::setCollatingLocale();
@@ -277,7 +296,7 @@ abstract class AbstractEntityRepository extends \TYPO3\CMS\Extbase\Persistence\R
                $dbFieldDefinitions = $installToolSqlParser->getFieldDefinitions_database();
                $dbFields = array();
                $dbFields[$tableName] = $dbFieldDefinitions[$tableName];
-               
+
                $extensionKey = \TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($this->extensionName);
                $extensionPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($extensionKey);
                $ext_tables = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($extensionPath . 'ext_tables.sql');
@@ -294,5 +313,62 @@ abstract class AbstractEntityRepository extends \TYPO3\CMS\Extbase\Persistence\R
                $databaseUtility = $this->objectManager->get('SJBR\\StaticInfoTables\\Utility\\DatabaseUtility');
                return $databaseUtility->dumpStaticTables($dbFields);
        }
-}
-?>
\ No newline at end of file
+
+       /**
+        * Adds an object to this repository.
+        *
+        * @param object $object The object to add
+        *
+        * @return void
+        *
+        * @throws \BadMethodCallException(
+        */
+       public function add($object) {
+               throw new \BadMethodCallException(
+                       'This is a read-only repository in which the add method must not be called.', 1420485488
+               );
+       }
+
+       /**
+        * Removes an object from this repository.
+        *
+        * @param object $object The object to remove
+        *
+        * @return void
+        *
+        * @throws \BadMethodCallException(
+        */
+       public function remove($object) {
+               throw new \BadMethodCallException(
+                       'This is a read-only repository in which the remove method must not be called.', 1420485646
+               );
+       }
+
+       /**
+        * Replaces an existing object with the same identifier by the given object.
+        *
+        * @param object $modifiedObject The modified object
+        *
+        * @return void
+        *
+        * @throws \BadMethodCallException(
+        */
+       public function update($modifiedObject) {
+               throw new \BadMethodCallException(
+                       'This is a read-only repository in which the update method must not be called.', 1420485660
+               );
+       }
+
+       /**
+        * Removes all objects of this repository as if remove() was called for all of them.
+        *
+        * @return void
+        *
+        * @throws \BadMethodCallException(
+        */
+       public function removeAll() {
+               throw new \BadMethodCallException(
+                       'This is a read-only repository in which the removeAll method must not be called.', 1420485668
+               );
+       }
+}
\ No newline at end of file
diff --git a/Tests/Unit/Domain/Repository/AbstractEntityRepositoryTest.php b/Tests/Unit/Domain/Repository/AbstractEntityRepositoryTest.php
new file mode 100644 (file)
index 0000000..c5cd0ed
--- /dev/null
@@ -0,0 +1,116 @@
+<?php
+namespace SJBR\StaticInfoTables\Tests\Unit\Domain\Repository;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use SJBR\StaticInfoTables\Domain\Model\AbstractEntity;
+use SJBR\StaticInfoTables\Domain\Repository\AbstractEntityRepository;
+use TYPO3\CMS\Core\Tests\UnitTestCase;
+use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
+
+/**
+ * Testcase.
+ *
+ * @author Oliver Klee <typo3-coding@oliverklee.de>
+ */
+class AbstractEntityRepositoryTest extends UnitTestCase {
+       /**
+        * @var AbstractEntityRepository|\PHPUnit_Framework_MockObject_MockObject
+        */
+       protected $subject = NULL;
+
+       protected function setUp() {
+               /** @var ObjectManagerInterface $objectManager */
+               $objectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
+               $this->subject = $this->getMockForAbstractClass(
+                       'SJBR\\StaticInfoTables\\Domain\\Repository\\AbstractEntityRepository', array($objectManager)
+               );
+       }
+
+       /**
+        * @test
+        */
+       public function initializeObjectSetsRespectStoragePidToFalse() {
+               /** @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject $objectManager */
+               $objectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
+               $subject = $this->getMockForAbstractClass(
+                       'SJBR\\StaticInfoTables\\Domain\\Repository\\AbstractEntityRepository', array($objectManager)
+               );
+
+               $querySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
+               $objectManager->expects($this->once())->method('get')
+                       ->with('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings')->will($this->returnValue($querySettings));
+               $querySettings->expects($this->once())->method('setRespectStoragePage')->with(FALSE);
+
+               /** @var AbstractEntityRepository $subject */
+               $subject->initializeObject();
+       }
+
+       /**
+        * @test
+        */
+       public function initializeObjectSetsDefaultQuerySettings() {
+               /** @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject $objectManager */
+               $objectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
+               $subject = $this->getMock(
+                       'SJBR\\StaticInfoTables\\Domain\\Repository\\AbstractEntityRepository',
+                       array('setDefaultQuerySettings'), array($objectManager)
+               );
+
+               $querySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
+               $objectManager->expects($this->once())->method('get')
+                       ->with('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings')->will($this->returnValue($querySettings));
+
+               $subject->expects($this->once())->method('setDefaultQuerySettings')->with($querySettings);
+
+               /** @var AbstractEntityRepository $subject */
+               $subject->initializeObject();
+       }
+
+       /**
+        * @test
+        *
+        * @expectedException \BadMethodCallException
+        */
+       public function addThrowsException() {
+               $this->subject->add(new AbstractEntity());
+       }
+
+       /**
+        * @test
+        *
+        * @expectedException \BadMethodCallException
+        */
+       public function removeThrowsException() {
+               $this->subject->remove(new AbstractEntity());
+       }
+
+       /**
+        * @test
+        *
+        * @expectedException \BadMethodCallException
+        */
+       public function updateThrowsException() {
+               $this->subject->update(new AbstractEntity());
+       }
+
+       /**
+        * @test
+        *
+        * @expectedException \BadMethodCallException
+        */
+       public function removeAllThrowsException() {
+               $this->subject->removeAll();
+       }
+}
\ No newline at end of file