diff --git a/app/code/Magento/Config/Model/Config/Backend/Admin/Robots.php b/app/code/Magento/Config/Model/Config/Backend/Admin/Robots.php index f6f969bd3bc90..a98e76285ce08 100644 --- a/app/code/Magento/Config/Model/Config/Backend/Admin/Robots.php +++ b/app/code/Magento/Config/Model/Config/Backend/Admin/Robots.php @@ -13,7 +13,7 @@ use Magento\Framework\App\ObjectManager; /** - * @api + * @deprecated robots.txt file is no longer stored in filesystem. It generates as response on request. */ class Robots extends \Magento\Framework\App\Config\Value { diff --git a/app/code/Magento/Customer/Block/CustomerScopeData.php b/app/code/Magento/Customer/Block/CustomerScopeData.php new file mode 100644 index 0000000000000..244437e870b98 --- /dev/null +++ b/app/code/Magento/Customer/Block/CustomerScopeData.php @@ -0,0 +1,53 @@ +storeManager = $context->getStoreManager(); + $this->jsonEncoder = $jsonEncoder; + } + + /** + * Return id of current website + * + * Can be used when necessary to obtain website id of the current customer. + * + * @return integer + */ + public function getWebsiteId() + { + return (int)$this->_storeManager->getStore()->getWebsiteId(); + } +} diff --git a/app/code/Magento/Customer/CustomerData/Customer.php b/app/code/Magento/Customer/CustomerData/Customer.php index afac3020fbcbb..35fcb63ceb157 100644 --- a/app/code/Magento/Customer/CustomerData/Customer.php +++ b/app/code/Magento/Customer/CustomerData/Customer.php @@ -19,6 +19,11 @@ class Customer implements SectionSourceInterface */ protected $currentCustomer; + /** + * @var View + */ + private $customerViewHelper; + /** * @param CurrentCustomer $currentCustomer * @param View $customerViewHelper @@ -39,10 +44,12 @@ public function getSectionData() if (!$this->currentCustomer->getCustomerId()) { return []; } + $customer = $this->currentCustomer->getCustomer(); return [ 'fullname' => $this->customerViewHelper->getCustomerName($customer), 'firstname' => $customer->getFirstname(), + 'websiteId' => $customer->getWebsiteId(), ]; } } diff --git a/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php b/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php new file mode 100644 index 0000000000000..dcbe4882231ca --- /dev/null +++ b/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php @@ -0,0 +1,81 @@ +contextMock = $this->getMockBuilder(Context::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) + ->getMock(); + + $this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class) + ->getMock(); + + $this->encoderMock = $this->getMockBuilder(EncoderInterface::class) + ->getMock(); + + $this->contextMock->expects($this->exactly(2)) + ->method('getStoreManager') + ->willReturn($this->storeManagerMock); + + $this->contextMock->expects($this->once()) + ->method('getScopeConfig') + ->willReturn($this->scopeConfigMock); + + $this->model = new CustomerScopeData( + $this->contextMock, + $this->encoderMock, + [] + ); + } + + public function testGetWebsiteId() + { + $storeId = 1; + + $storeMock = $this->getMockBuilder(StoreInterface::class) + ->setMethods(['getWebsiteId']) + ->getMockForAbstractClass(); + + $storeMock->expects($this->any()) + ->method('getWebsiteId') + ->willReturn($storeId); + + $this->storeManagerMock->expects($this->any()) + ->method('getStore') + ->with(null) + ->willReturn($storeMock); + + $this->assertEquals($storeId, $this->model->getWebsiteId()); + } +} diff --git a/app/code/Magento/Customer/view/frontend/layout/default.xml b/app/code/Magento/Customer/view/frontend/layout/default.xml index c6d8ae5371fc6..94e46fda194b0 100644 --- a/app/code/Magento/Customer/view/frontend/layout/default.xml +++ b/app/code/Magento/Customer/view/frontend/layout/default.xml @@ -45,6 +45,8 @@ + diff --git a/app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml b/app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml new file mode 100644 index 0000000000000..7905b1d9925e3 --- /dev/null +++ b/app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml @@ -0,0 +1,29 @@ + + + diff --git a/app/code/Magento/Customer/view/frontend/web/js/invalidation-processor.js b/app/code/Magento/Customer/view/frontend/web/js/invalidation-processor.js new file mode 100644 index 0000000000000..d99574ec3dfbf --- /dev/null +++ b/app/code/Magento/Customer/view/frontend/web/js/invalidation-processor.js @@ -0,0 +1,41 @@ +/** + * Copyright © 2013-2017 Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +define([ + 'underscore', + 'uiElement', + 'Magento_Customer/js/customer-data' +], function (_, Element, customerData) { + 'use strict'; + + return Element.extend({ + /** + * Initialize object + */ + initialize: function () { + this._super(); + this.process(customerData); + }, + + /** + * Process all rules in loop, each rule can invalidate some sections in customer data + * + * @param {Object} customerDataObject + */ + process: function (customerDataObject) { + _.each(this.invalidationRules, function (rule, ruleName) { + _.each(rule, function (ruleArgs, rulePath) { + require([rulePath], function (Rule) { + var currentRule = new Rule(ruleArgs); + + if (!_.isFunction(currentRule.process)) { + throw new Error('Rule ' + ruleName + ' should implement invalidationProcessor interface'); + } + currentRule.process(customerDataObject); + }); + }); + }); + } + }); +}); diff --git a/app/code/Magento/Customer/view/frontend/web/js/invalidation-rules/website-rule.js b/app/code/Magento/Customer/view/frontend/web/js/invalidation-rules/website-rule.js new file mode 100644 index 0000000000000..eb7f101a6d47e --- /dev/null +++ b/app/code/Magento/Customer/view/frontend/web/js/invalidation-rules/website-rule.js @@ -0,0 +1,31 @@ +/** + * Copyright © 2013-2017 Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +define([ + 'uiClass' +], function (Element) { + 'use strict'; + + return Element.extend({ + + defaults: { + scopeConfig: {} + }, + + /** + * Takes website id from current customer data and compare it with current website id + * If customer belongs to another scope, we need to invalidate current section + * + * @param {Object} customerData + */ + process: function (customerData) { + var customer = customerData.get('customer'); + + if (this.scopeConfig && customer() && + ~~customer().websiteId !== ~~this.scopeConfig.websiteId && ~~customer().websiteId !== 0) { + customerData.reload(['customer']); + } + } + }); +}); diff --git a/app/code/Magento/Robots/Block/Data.php b/app/code/Magento/Robots/Block/Data.php new file mode 100644 index 0000000000000..5426bbaf148d6 --- /dev/null +++ b/app/code/Magento/Robots/Block/Data.php @@ -0,0 +1,72 @@ +robots = $robots; + $this->storeResolver = $storeResolver; + + parent::__construct($context, $data); + } + + /** + * Retrieve base content for robots.txt file + * + * @return string + */ + protected function _toHtml() + { + return $this->robots->getData() . PHP_EOL; + } + + /** + * Get unique page cache identities + * + * @return array + */ + public function getIdentities() + { + return [ + Value::CACHE_TAG . '_' . $this->storeResolver->getCurrentStoreId(), + ]; + } +} diff --git a/app/code/Magento/Robots/Controller/Index/Index.php b/app/code/Magento/Robots/Controller/Index/Index.php new file mode 100644 index 0000000000000..b94626e93432d --- /dev/null +++ b/app/code/Magento/Robots/Controller/Index/Index.php @@ -0,0 +1,48 @@ +resultPageFactory = $resultPageFactory; + + parent::__construct($context); + } + + /** + * Generates robots.txt data and returns it as result + * + * @return Page + */ + public function execute() + { + /** @var Page $resultPage */ + $resultPage = $this->resultPageFactory->create(true); + $resultPage->addHandle('robots_index_index'); + return $resultPage; + } +} diff --git a/app/code/Magento/Robots/Controller/Router.php b/app/code/Magento/Robots/Controller/Router.php new file mode 100644 index 0000000000000..4ed2b6c72871b --- /dev/null +++ b/app/code/Magento/Robots/Controller/Router.php @@ -0,0 +1,72 @@ +actionFactory = $actionFactory; + $this->actionList = $actionList; + $this->routeConfig = $routeConfig; + } + + /** + * Checks if robots.txt file was requested and returns instance of matched application action class + * + * @param RequestInterface $request + * @return ActionInterface|null + */ + public function match(RequestInterface $request) + { + $identifier = trim($request->getPathInfo(), '/'); + if ($identifier !== 'robots.txt') { + return null; + } + + $modules = $this->routeConfig->getModulesByFrontName('robots'); + if (empty($modules)) { + return null; + } + + $actionClassName = $this->actionList->get($modules[0], null, 'index', 'index'); + $actionInstance = $this->actionFactory->create($actionClassName); + return $actionInstance; + } +} diff --git a/app/code/Magento/Robots/LICENSE.txt b/app/code/Magento/Robots/LICENSE.txt new file mode 100644 index 0000000000000..49525fd99da9c --- /dev/null +++ b/app/code/Magento/Robots/LICENSE.txt @@ -0,0 +1,48 @@ + +Open Software License ("OSL") v. 3.0 + +This Open Software License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work: + +Licensed under the Open Software License version 3.0 + + 1. Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following: + + 1. to reproduce the Original Work in copies, either alone or as part of a collective work; + + 2. to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work; + + 3. to distribute or communicate copies of the Original Work and Derivative Works to the public, with the proviso that copies of Original Work or Derivative Works that You distribute or communicate shall be licensed under this Open Software License; + + 4. to perform the Original Work publicly; and + + 5. to display the Original Work publicly. + + 2. Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works. + + 3. Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work. + + 4. Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor's trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license. + + 5. External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c). + + 6. Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. + + 7. Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer. + + 8. Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation. + + 9. Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including 'fair use' or 'fair dealing'). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c). + + 10. Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. + + 11. Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License. + + 12. Attorneys' Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. + + 13. Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. + + 14. Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + + 15. Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. + + 16. Modification of This License. This License is Copyright (C) 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Open Software License" or "OSL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under " or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process. \ No newline at end of file diff --git a/app/code/Magento/Robots/LICENSE_AFL.txt b/app/code/Magento/Robots/LICENSE_AFL.txt new file mode 100644 index 0000000000000..f39d641b18a19 --- /dev/null +++ b/app/code/Magento/Robots/LICENSE_AFL.txt @@ -0,0 +1,48 @@ + +Academic Free License ("AFL") v. 3.0 + +This Academic Free License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work: + +Licensed under the Academic Free License version 3.0 + + 1. Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following: + + 1. to reproduce the Original Work in copies, either alone or as part of a collective work; + + 2. to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work; + + 3. to distribute or communicate copies of the Original Work and Derivative Works to the public, under any license of your choice that does not contradict the terms and conditions, including Licensor's reserved rights and remedies, in this Academic Free License; + + 4. to perform the Original Work publicly; and + + 5. to display the Original Work publicly. + + 2. Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works. + + 3. Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work. + + 4. Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor's trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license. + + 5. External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c). + + 6. Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. + + 7. Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer. + + 8. Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation. + + 9. Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including "fair use" or "fair dealing"). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c). + + 10. Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. + + 11. Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License. + + 12. Attorneys' Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. + + 13. Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. + + 14. Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + + 15. Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. + + 16. Modification of This License. This License is Copyright © 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Academic Free License" or "AFL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under " or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process. diff --git a/app/code/Magento/Robots/Model/Config/Value.php b/app/code/Magento/Robots/Model/Config/Value.php new file mode 100644 index 0000000000000..9e837965fa254 --- /dev/null +++ b/app/code/Magento/Robots/Model/Config/Value.php @@ -0,0 +1,87 @@ +storeResolver = $storeResolver; + + parent::__construct( + $context, + $registry, + $config, + $cacheTypeList, + $resource, + $resourceCollection, + $data + ); + } + + /** + * Get unique page cache identities + * + * @return array + */ + public function getIdentities() + { + return [ + self::CACHE_TAG . '_' . $this->storeResolver->getCurrentStoreId(), + ]; + } +} diff --git a/app/code/Magento/Robots/Model/Robots.php b/app/code/Magento/Robots/Model/Robots.php new file mode 100644 index 0000000000000..7f36ec5703212 --- /dev/null +++ b/app/code/Magento/Robots/Model/Robots.php @@ -0,0 +1,42 @@ +scopeConfig = $scopeConfig; + } + + /** + * Get the main data for robots.txt file as defined in configuration + * + * @return string + */ + public function getData() + { + return $this->scopeConfig->getValue( + 'design/search_engine_robots/custom_instructions', + ScopeInterface::SCOPE_WEBSITE + ); + } +} diff --git a/app/code/Magento/Robots/README.md b/app/code/Magento/Robots/README.md new file mode 100644 index 0000000000000..936dbe973a3ee --- /dev/null +++ b/app/code/Magento/Robots/README.md @@ -0,0 +1,3 @@ +The Robots module provides the following functionalities: +* contains a router to match application action class for requests to the `robots.txt` file; +* allows obtaining the content of the `robots.txt` file depending on the settings of the current website. diff --git a/app/code/Magento/Robots/Test/Unit/Block/DataTest.php b/app/code/Magento/Robots/Test/Unit/Block/DataTest.php new file mode 100644 index 0000000000000..b64f519617a7e --- /dev/null +++ b/app/code/Magento/Robots/Test/Unit/Block/DataTest.php @@ -0,0 +1,122 @@ +eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class) + ->getMockForAbstractClass(); + + $this->context = $this->getMockBuilder(\Magento\Framework\View\Element\Context::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->context->expects($this->any()) + ->method('getEventManager') + ->willReturn($this->eventManagerMock); + + $this->robots = $this->getMockBuilder(\Magento\Robots\Model\Robots::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->storeResolver = $this->getMockBuilder(\Magento\Store\Model\StoreResolver::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->block = new \Magento\Robots\Block\Data( + $this->context, + $this->robots, + $this->storeResolver + ); + } + + /** + * Check that toHtml() method returns specified text data + */ + public function testToHtml() + { + $data = 'test'; + + $this->initEventManagerMock($data); + + $this->robots->expects($this->once()) + ->method('getData') + ->willReturn($data); + + $this->assertEquals($data . PHP_EOL, $this->block->toHtml()); + } + + /** + * Check that getIdentities() method returns specified cache tag + */ + public function testGetIdentities() + { + $storeId = 1; + + $this->storeResolver->expects($this->once()) + ->method('getCurrentStoreId') + ->willReturn($storeId); + + $expected = [ + \Magento\Robots\Model\Config\Value::CACHE_TAG . '_' . $storeId, + ]; + $this->assertEquals($expected, $this->block->getIdentities()); + } + + /** + * Initialize mock object of Event Manager + * + * @param string $data + * @return void + */ + protected function initEventManagerMock($data) + { + $this->eventManagerMock->expects($this->any()) + ->method('dispatch') + ->willReturnMap([ + [ + 'view_block_abstract_to_html_before', + [ + 'block' => $this->block, + ], + ], + [ + 'view_block_abstract_to_html_after', + [ + 'block' => $this->block, + 'transport' => new \Magento\Framework\DataObject(['html' => $data]), + ], + ], + ]); + } +} diff --git a/app/code/Magento/Robots/Test/Unit/Controller/Index/IndexTest.php b/app/code/Magento/Robots/Test/Unit/Controller/Index/IndexTest.php new file mode 100644 index 0000000000000..aaac511e3c521 --- /dev/null +++ b/app/code/Magento/Robots/Test/Unit/Controller/Index/IndexTest.php @@ -0,0 +1,65 @@ +contextMock = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->resultPageFactory = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class) + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + + $this->controller = new \Magento\Robots\Controller\Index\Index( + $this->contextMock, + $this->resultPageFactory + ); + } + + /** + * Check the basic flow of execute() method + */ + public function testExecute() + { + $resultPageMock = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class) + ->disableOriginalConstructor() + ->getMock(); + $resultPageMock->expects($this->once()) + ->method('addHandle') + ->with('robots_index_index'); + + $this->resultPageFactory->expects($this->any()) + ->method('create') + ->with(true) + ->willReturn($resultPageMock); + + $this->assertInstanceOf( + \Magento\Framework\View\Result\Page::class, + $this->controller->execute() + ); + } +} diff --git a/app/code/Magento/Robots/Test/Unit/Controller/RouterTest.php b/app/code/Magento/Robots/Test/Unit/Controller/RouterTest.php new file mode 100644 index 0000000000000..a2941dac44550 --- /dev/null +++ b/app/code/Magento/Robots/Test/Unit/Controller/RouterTest.php @@ -0,0 +1,128 @@ +actionFactoryMock = $this->getMockBuilder(\Magento\Framework\App\ActionFactory::class) + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + + $this->actionListMock = $this->getMockBuilder(\Magento\Framework\App\Router\ActionList::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->routeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Route\ConfigInterface::class) + ->getMockForAbstractClass(); + + $this->router = new \Magento\Robots\Controller\Router( + $this->actionFactoryMock, + $this->actionListMock, + $this->routeConfigMock + ); + } + + /** + * Check case when robots.txt file is not requested + */ + public function testMatchNoRobotsRequested() + { + $identifier = 'test'; + + $requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class) + ->setMethods(['getPathInfo']) + ->getMockForAbstractClass(); + $requestMock->expects($this->once()) + ->method('getPathInfo') + ->willReturn($identifier); + + $this->assertNull($this->router->match($requestMock)); + } + + /** + * Check case, when no existed modules in Magento to process 'robots' route + */ + public function testMatchNoRobotsModules() + { + $identifier = 'robots.txt'; + + $requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class) + ->setMethods(['getPathInfo']) + ->getMockForAbstractClass(); + $requestMock->expects($this->once()) + ->method('getPathInfo') + ->willReturn($identifier); + + $this->routeConfigMock->expects($this->once()) + ->method('getModulesByFrontName') + ->with('robots') + ->willReturn([]); + + $this->assertNull($this->router->match($requestMock)); + } + + /** + * Check the basic flow of match() method + */ + public function testMatch() + { + $identifier = 'robots.txt'; + $moduleName = 'Magento_Robots'; + $actionClassName = \Magento\Robots\Controller\Index\Index::class; + + $requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class) + ->setMethods(['getPathInfo']) + ->getMockForAbstractClass(); + $requestMock->expects($this->once()) + ->method('getPathInfo') + ->willReturn($identifier); + + $this->routeConfigMock->expects($this->once()) + ->method('getModulesByFrontName') + ->with('robots') + ->willReturn([$moduleName]); + + $this->actionListMock->expects($this->once()) + ->method('get') + ->with($moduleName, null, 'index', 'index') + ->willReturn($actionClassName); + + $actionClassMock = $this->getMockBuilder(\Magento\Robots\Controller\Index\Index::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->actionFactoryMock->expects($this->once()) + ->method('create') + ->with($actionClassName) + ->willReturn($actionClassMock); + + $this->assertInstanceOf($actionClassName, $this->router->match($requestMock)); + } +} diff --git a/app/code/Magento/Robots/Test/Unit/Model/Config/ValueTest.php b/app/code/Magento/Robots/Test/Unit/Model/Config/ValueTest.php new file mode 100644 index 0000000000000..a61cc42075191 --- /dev/null +++ b/app/code/Magento/Robots/Test/Unit/Model/Config/ValueTest.php @@ -0,0 +1,85 @@ +context = $this->getMockBuilder(\Magento\Framework\Model\Context::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class) + ->getMockForAbstractClass(); + + $this->typeList = $this->getMockBuilder(\Magento\Framework\App\Cache\TypeListInterface::class) + ->getMockForAbstractClass(); + + $this->storeResolver = $this->getMockBuilder(\Magento\Store\Model\StoreResolver::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->model = new \Magento\Robots\Model\Config\Value( + $this->context, + $this->registry, + $this->scopeConfig, + $this->typeList, + $this->storeResolver + ); + } + + /** + * Check that getIdentities() method returns specified cache tag + */ + public function testGetIdentities() + { + $storeId = 1; + + $this->storeResolver->expects($this->once()) + ->method('getCurrentStoreId') + ->willReturn($storeId); + + $expected = [ + \Magento\Robots\Model\Config\Value::CACHE_TAG . '_' . $storeId, + ]; + $this->assertEquals($expected, $this->model->getIdentities()); + } +} diff --git a/app/code/Magento/Robots/Test/Unit/Model/RobotsTest.php b/app/code/Magento/Robots/Test/Unit/Model/RobotsTest.php new file mode 100644 index 0000000000000..68f36d4579039 --- /dev/null +++ b/app/code/Magento/Robots/Test/Unit/Model/RobotsTest.php @@ -0,0 +1,51 @@ +scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class) + ->getMockForAbstractClass(); + + $this->model = new Robots( + $this->scopeConfigMock + ); + } + + /** + * Check general logic of getData() method + */ + public function testGetData() + { + $customInstructions = 'custom_instructions'; + + $this->scopeConfigMock->expects($this->once()) + ->method('getValue') + ->with( + 'design/search_engine_robots/custom_instructions', + ScopeInterface::SCOPE_WEBSITE + ) + ->willReturn($customInstructions); + + $this->assertEquals($customInstructions, $this->model->getData()); + } +} diff --git a/app/code/Magento/Robots/composer.json b/app/code/Magento/Robots/composer.json new file mode 100644 index 0000000000000..a359918fad7d1 --- /dev/null +++ b/app/code/Magento/Robots/composer.json @@ -0,0 +1,26 @@ +{ + "name": "magento/module-robots", + "description": "N/A", + "require": { + "php": "7.0.2|7.0.4|~7.0.6|~7.1.0", + "magento/framework": "100.2.*", + "magento/module-store": "100.2.*" + }, + "suggest": { + "magento/module-theme": "100.2.*" + }, + "type": "magento2-module", + "version": "100.2.0-dev", + "license": [ + "OSL-3.0", + "AFL-3.0" + ], + "autoload": { + "files": [ + "registration.php" + ], + "psr-4": { + "Magento\\Robots\\": "" + } + } +} diff --git a/app/code/Magento/Robots/etc/di.xml b/app/code/Magento/Robots/etc/di.xml new file mode 100644 index 0000000000000..27a8fa7d0c011 --- /dev/null +++ b/app/code/Magento/Robots/etc/di.xml @@ -0,0 +1,33 @@ + + + + + + Magento_Robots::robots.phtml + + + + + robotsResultPage + + + + + robotsResultPageFactory + + + + + + + Magento\Robots\Model\Config\Value + + + + + diff --git a/app/code/Magento/Robots/etc/frontend/di.xml b/app/code/Magento/Robots/etc/frontend/di.xml new file mode 100644 index 0000000000000..5670ee637beeb --- /dev/null +++ b/app/code/Magento/Robots/etc/frontend/di.xml @@ -0,0 +1,21 @@ + + + + + + + + Magento\Robots\Controller\Router + false + 10 + + + + + diff --git a/app/code/Magento/Robots/etc/frontend/routes.xml b/app/code/Magento/Robots/etc/frontend/routes.xml new file mode 100644 index 0000000000000..a30cbe320f673 --- /dev/null +++ b/app/code/Magento/Robots/etc/frontend/routes.xml @@ -0,0 +1,15 @@ + + + + + + + + + diff --git a/app/code/Magento/Robots/etc/module.xml b/app/code/Magento/Robots/etc/module.xml new file mode 100644 index 0000000000000..ab04dfb7486f7 --- /dev/null +++ b/app/code/Magento/Robots/etc/module.xml @@ -0,0 +1,16 @@ + + + + + + + + + + diff --git a/app/code/Magento/Robots/registration.php b/app/code/Magento/Robots/registration.php new file mode 100644 index 0000000000000..0e062e1139461 --- /dev/null +++ b/app/code/Magento/Robots/registration.php @@ -0,0 +1,11 @@ + + + + + + + + + diff --git a/app/code/Magento/Robots/view/frontend/page_layout/robots.xml b/app/code/Magento/Robots/view/frontend/page_layout/robots.xml new file mode 100644 index 0000000000000..1184772e6817d --- /dev/null +++ b/app/code/Magento/Robots/view/frontend/page_layout/robots.xml @@ -0,0 +1,10 @@ + + + + + diff --git a/app/code/Magento/Robots/view/frontend/templates/robots.phtml b/app/code/Magento/Robots/view/frontend/templates/robots.phtml new file mode 100644 index 0000000000000..4d5ef94cd565c --- /dev/null +++ b/app/code/Magento/Robots/view/frontend/templates/robots.phtml @@ -0,0 +1,6 @@ +ruleFactory = $ruleFactory; $this->ruleDataFactory = $ruleDataFactory; @@ -65,16 +74,23 @@ public function __construct( $this->ruleLabelFactory = $ruleLabelFactory; $this->dataObjectProcessor = $dataObjectProcessor; $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Json::class); + $this->extensionFactory = $extensionFactory ?: + \Magento\Framework\App\ObjectManager::getInstance()->get(RuleExtensionFactory::class); } /** + * Converts Sale Rule model to Sale Rule DTO + * * @param Rule $ruleModel * @return RuleDataModel */ - public function toDataModel(\Magento\SalesRule\Model\Rule $ruleModel) + public function toDataModel(Rule $ruleModel) { + $modelData = $ruleModel->getData(); + $modelData = $this->convertExtensionAttributesToObject($modelData); + /** @var \Magento\SalesRule\Model\Data\Rule $dataModel */ - $dataModel = $this->ruleDataFactory->create(['data' => $ruleModel->getData()]); + $dataModel = $this->ruleDataFactory->create(['data' => $modelData]); $this->mapFields($dataModel, $ruleModel); @@ -83,10 +99,10 @@ public function toDataModel(\Magento\SalesRule\Model\Rule $ruleModel) /** * @param RuleDataModel $dataModel - * @param \Magento\SalesRule\Model\Rule $ruleModel + * @param Rule $ruleModel * @return $this */ - protected function mapConditions(RuleDataModel $dataModel, \Magento\SalesRule\Model\Rule $ruleModel) + protected function mapConditions(RuleDataModel $dataModel, Rule $ruleModel) { $conditionSerialized = $ruleModel->getConditionsSerialized(); if ($conditionSerialized) { @@ -101,10 +117,10 @@ protected function mapConditions(RuleDataModel $dataModel, \Magento\SalesRule\Mo /** * @param RuleDataModel $dataModel - * @param \Magento\SalesRule\Model\Rule $ruleModel + * @param Rule $ruleModel * @return $this */ - protected function mapActionConditions(RuleDataModel $dataModel, \Magento\SalesRule\Model\Rule $ruleModel) + protected function mapActionConditions(RuleDataModel $dataModel, Rule $ruleModel) { $actionConditionSerialized = $ruleModel->getActionsSerialized(); if ($actionConditionSerialized) { @@ -162,12 +178,27 @@ protected function mapCouponType(RuleDataModel $dataModel) return $this; } + /** + * Convert extension attributes of model to object if it is an array + * + * @param array $data + * @return array + */ + private function convertExtensionAttributesToObject(array $data) + { + if (isset($data['extension_attributes']) && is_array($data['extension_attributes'])) { + /** @var RuleExtensionInterface $attributes */ + $data['extension_attributes'] = $this->extensionFactory->create(['data' => $data['extension_attributes']]); + } + return $data; + } + /** * @param RuleDataModel $dataModel - * @param \Magento\SalesRule\Model\Rule $ruleModel + * @param Rule $ruleModel * @return $this */ - protected function mapFields(RuleDataModel $dataModel, \Magento\SalesRule\Model\Rule $ruleModel) + protected function mapFields(RuleDataModel $dataModel, Rule $ruleModel) { $this->mapConditions($dataModel, $ruleModel); $this->mapActionConditions($dataModel, $ruleModel); diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/Converter/ToDataModelTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/Converter/ToDataModelTest.php index d14f85a4330e5..f58ea9081ad5e 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/Converter/ToDataModelTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/Converter/ToDataModelTest.php @@ -5,6 +5,9 @@ */ namespace Magento\SalesRule\Test\Unit\Model\Converter; +use Magento\SalesRule\Api\Data\RuleExtensionFactory; +use Magento\SalesRule\Api\Data\RuleExtensionInterface; + /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ @@ -50,6 +53,11 @@ class ToDataModelTest extends \PHPUnit_Framework_TestCase */ protected $serializer; + /** + * @var RuleExtensionFactory|\PHPUnit_Framework_MockObject_MockObject + */ + private $extensionFactoryMock; + protected function setUp() { $this->ruleFactory = $this->getMockBuilder(\Magento\SalesRule\Model\RuleFactory::class) @@ -87,6 +95,11 @@ protected function setUp() ->setMethods(null) ->getMock(); + $this->extensionFactoryMock = $this->getMockBuilder(RuleExtensionFactory::class) + ->setMethods(['create']) + ->disableOriginalConstructor() + ->getMock(); + $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->model = $helper->getObject( \Magento\SalesRule\Model\Converter\ToDataModel::class, @@ -97,6 +110,7 @@ protected function setUp() 'ruleLabelFactory' => $this->ruleLabelFactory, 'dataObjectProcessor' => $this->dataObjectProcessor, 'serializer' => $this->serializer, + 'extensionFactory' => $this->extensionFactoryMock, ] ); } @@ -147,12 +161,27 @@ private function getArrayData() 0 => 'TestRule', 1 => 'TestRuleForDefaultStore', ], + 'extension_attributes' => [ + 'some_extension_attributes' => 123, + ], ]; } public function testToDataModel() { $array = $this->getArrayData(); + $arrayAttributes = $array; + + /** @var RuleExtensionInterface|\PHPUnit_Framework_MockObject_MockObject $attributesMock */ + $attributesMock = $this->getMockBuilder(RuleExtensionInterface::class) + ->getMock(); + $arrayAttributes['extension_attributes'] = $attributesMock; + + $this->extensionFactoryMock->expects($this->any()) + ->method('create') + ->with(['data' => $array['extension_attributes']]) + ->willReturn($attributesMock); + $dataModel = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Rule::class) ->disableOriginalConstructor() ->setMethods(['create', 'getStoreLabels', 'setStoreLabels', 'getCouponType', 'setCouponType']) @@ -181,6 +210,7 @@ public function testToDataModel() $this->ruleDataFactory ->expects($this->any()) ->method('create') + ->with(['data' => $arrayAttributes]) ->willReturn($dataModel); $this->salesRule diff --git a/app/code/Magento/Sitemap/Block/Robots.php b/app/code/Magento/Sitemap/Block/Robots.php new file mode 100644 index 0000000000000..4b50f6eb586a3 --- /dev/null +++ b/app/code/Magento/Sitemap/Block/Robots.php @@ -0,0 +1,140 @@ +storeResolver = $storeResolver; + $this->sitemapCollectionFactory = $sitemapCollectionFactory; + $this->sitemapHelper = $sitemapHelper; + $this->storeManager = $storeManager; + + parent::__construct($context, $data); + } + + /** + * Prepare sitemap links to add to the robots.txt file + * + * Collects sitemap links for all stores of given website. + * Detects if sitemap file information is required to be added to robots.txt + * and adds links for this sitemap files into result data. + * + * @return string + */ + protected function _toHtml() + { + $defaultStoreId = $this->storeResolver->getCurrentStoreId(); + $defalutStore = $this->storeManager->getStore($defaultStoreId); + + /** @var \Magento\Store\Model\Website $website */ + $website = $this->storeManager->getWebsite($defalutStore->getWebsiteId()); + + $storeIds = []; + foreach ($website->getStoreIds() as $storeId) { + if ((bool)$this->sitemapHelper->getEnableSubmissionRobots($storeId)) { + $storeIds[] = (int)$storeId; + } + } + + $links = []; + if ($storeIds) { + $links = array_merge($links, $this->getSitemapLinks($storeIds)); + } + + return $links ? implode(PHP_EOL, $links) . PHP_EOL : ''; + } + + /** + * Retrieve sitemap links for given store + * + * Gets the names of sitemap files that linked with given store, + * and adds links for this sitemap files into result array. + * + * @param int[] $storeIds + * @return array + */ + protected function getSitemapLinks(array $storeIds) + { + $sitemapLinks = []; + + /** @var \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection $collection */ + $collection = $this->sitemapCollectionFactory->create(); + $collection->addStoreFilter($storeIds); + + foreach ($collection as $sitemap) { + /** @var \Magento\Sitemap\Model\Sitemap $sitemap */ + $sitemapFilename = $sitemap->getSitemapFilename(); + $sitemapPath = $sitemap->getSitemapPath(); + + $sitemapUrl = $sitemap->getSitemapUrl($sitemapPath, $sitemapFilename); + $sitemapLinks[$sitemapUrl] = 'Sitemap: ' . $sitemapUrl; + } + + return $sitemapLinks; + } + + /** + * Get unique page cache identities + * + * @return array + */ + public function getIdentities() + { + return [ + Value::CACHE_TAG . '_' . $this->storeResolver->getCurrentStoreId(), + ]; + } +} diff --git a/app/code/Magento/Sitemap/Model/Config/Backend/Robots.php b/app/code/Magento/Sitemap/Model/Config/Backend/Robots.php new file mode 100644 index 0000000000000..d69b8e6d44815 --- /dev/null +++ b/app/code/Magento/Sitemap/Model/Config/Backend/Robots.php @@ -0,0 +1,81 @@ +storeResolver = $storeResolver; + + parent::__construct( + $context, + $registry, + $config, + $cacheTypeList, + $resource, + $resourceCollection, + $data + ); + } + + /** + * Get unique page cache identities + * + * @return array + */ + public function getIdentities() + { + return [ + RobotsValue::CACHE_TAG . '_' . $this->storeResolver->getCurrentStoreId(), + ]; + } +} diff --git a/app/code/Magento/Sitemap/Model/Sitemap.php b/app/code/Magento/Sitemap/Model/Sitemap.php index 2657a90704567..ef8363cbcf2ff 100644 --- a/app/code/Magento/Sitemap/Model/Sitemap.php +++ b/app/code/Magento/Sitemap/Model/Sitemap.php @@ -10,6 +10,7 @@ use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot; use Magento\Framework\App\ObjectManager; +use Magento\Robots\Model\Config\Value; use Magento\Framework\DataObject; /** @@ -31,7 +32,7 @@ * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @api */ -class Sitemap extends \Magento\Framework\Model\AbstractModel +class Sitemap extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface { const OPEN_TAG_KEY = 'start'; @@ -149,6 +150,13 @@ class Sitemap extends \Magento\Framework\Model\AbstractModel */ protected $dateTime; + /** + * Model cache tag for clear cache in after save and after delete + * + * @var string + */ + protected $_cacheTag = true; + /** * Initialize dependencies. * @@ -199,6 +207,7 @@ public function __construct( $this->_storeManager = $storeManager; $this->_request = $request; $this->dateTime = $dateTime; + parent::__construct($context, $registry, $resource, $resourceCollection, $data); } @@ -410,11 +419,6 @@ public function generateXml() $this->_createSitemapIndex(); } - // Push sitemap to robots.txt - if ($this->_isEnabledSubmissionRobots()) { - $this->_addSitemapToRobotsTxt($this->getSitemapFilename()); - } - $this->setSitemapTime($this->_dateModel->gmtDate('Y-m-d H:i:s')); $this->save(); @@ -710,6 +714,8 @@ public function getSitemapUrl($sitemapPath, $sitemapFileName) * Check is enabled submission to robots.txt * * @return bool + * @deprecated Because the robots.txt file is not generated anymore, + * this method is not needed and will be removed in major release. */ protected function _isEnabledSubmissionRobots() { @@ -724,6 +730,8 @@ protected function _isEnabledSubmissionRobots() * * @param string $sitemapFileName * @return void + * @deprecated Because the robots.txt file is not generated anymore, + * this method is not needed and will be removed in major release. */ protected function _addSitemapToRobotsTxt($sitemapFileName) { @@ -761,4 +769,16 @@ private function _findNewLinesDelimiter($text) return PHP_EOL; } + + /** + * Get unique page cache identities + * + * @return array + */ + public function getIdentities() + { + return [ + Value::CACHE_TAG . '_' . $this->getStoreId(), + ]; + } } diff --git a/app/code/Magento/Sitemap/Test/Unit/Block/RobotsTest.php b/app/code/Magento/Sitemap/Test/Unit/Block/RobotsTest.php new file mode 100644 index 0000000000000..7368442cad581 --- /dev/null +++ b/app/code/Magento/Sitemap/Test/Unit/Block/RobotsTest.php @@ -0,0 +1,285 @@ +eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class) + ->getMockForAbstractClass(); + + $this->context = $this->getMockBuilder(\Magento\Framework\View\Element\Context::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->context->expects($this->any()) + ->method('getEventManager') + ->willReturn($this->eventManagerMock); + + $this->storeResolver = $this->getMockBuilder(\Magento\Store\Model\StoreResolver::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->sitemapCollectionFactory = $this->getMockBuilder( + \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory::class + ) + ->disableOriginalConstructor() + ->getMock(); + + $this->sitemapHelper = $this->getMockBuilder(\Magento\Sitemap\Helper\Data::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class) + ->getMockForAbstractClass(); + + $this->block = new \Magento\Sitemap\Block\Robots( + $this->context, + $this->storeResolver, + $this->sitemapCollectionFactory, + $this->sitemapHelper, + $this->storeManager + ); + } + + /** + * Check toHtml() method in case when robots submission is disabled + */ + public function testToHtmlRobotsSubmissionIsDisabled() + { + $defaultStoreId = 1; + $defaultWebsiteId = 1; + + $expected = ''; + + $this->initEventManagerMock($expected); + + $this->storeResolver->expects($this->once()) + ->method('getCurrentStoreId') + ->willReturn($defaultStoreId); + + $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class) + ->getMockForAbstractClass(); + $storeMock->expects($this->any()) + ->method('getWebsiteId') + ->willReturn($defaultWebsiteId); + + $websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class) + ->disableOriginalConstructor() + ->getMock(); + $websiteMock->expects($this->any()) + ->method('getStoreIds') + ->willReturn([$defaultStoreId]); + + $this->storeManager->expects($this->once()) + ->method('getStore') + ->with($defaultStoreId) + ->willReturn($storeMock); + $this->storeManager->expects($this->once()) + ->method('getWebsite') + ->with($defaultWebsiteId) + ->willReturn($websiteMock); + + $this->sitemapHelper->expects($this->once()) + ->method('getEnableSubmissionRobots') + ->with($defaultStoreId) + ->willReturn(false); + + $this->assertEquals($expected, $this->block->toHtml()); + } + + /** + * Check toHtml() method in case when robots submission is enabled + */ + public function testAfterGetDataRobotsSubmissionIsEnabled() + { + $defaultStoreId = 1; + $secondStoreId = 2; + $defaultWebsiteId = 1; + + $sitemapPath = '/'; + $sitemapFilenameOne = 'sitemap.xml'; + $sitemapFilenameTwo = 'sitemap_custom.xml'; + $sitemapFilenameThree = 'sitemap.xml'; + + $expected = 'Sitemap: ' . $sitemapFilenameOne + . PHP_EOL + . 'Sitemap: ' . $sitemapFilenameTwo + . PHP_EOL; + + $this->initEventManagerMock($expected); + + $this->storeResolver->expects($this->once()) + ->method('getCurrentStoreId') + ->willReturn($defaultStoreId); + + $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class) + ->getMockForAbstractClass(); + $storeMock->expects($this->any()) + ->method('getWebsiteId') + ->willReturn($defaultWebsiteId); + + $websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class) + ->disableOriginalConstructor() + ->getMock(); + $websiteMock->expects($this->any()) + ->method('getStoreIds') + ->willReturn([$defaultStoreId, $secondStoreId]); + + $this->storeManager->expects($this->once()) + ->method('getStore') + ->with($defaultStoreId) + ->willReturn($storeMock); + $this->storeManager->expects($this->once()) + ->method('getWebsite') + ->with($defaultWebsiteId) + ->willReturn($websiteMock); + + $this->sitemapHelper->expects($this->any()) + ->method('getEnableSubmissionRobots') + ->willReturnMap([ + [$defaultStoreId, true], + [$secondStoreId, false], + ]); + + $sitemapMockOne = $this->getSitemapMock($sitemapPath, $sitemapFilenameOne); + $sitemapMockTwo = $this->getSitemapMock($sitemapPath, $sitemapFilenameTwo); + $sitemapMockThree = $this->getSitemapMock($sitemapPath, $sitemapFilenameThree); + + $sitemapCollectionMock = $this->getMockBuilder(\Magento\Sitemap\Model\ResourceModel\Sitemap\Collection::class) + ->disableOriginalConstructor() + ->getMock(); + $sitemapCollectionMock->expects($this->any()) + ->method('addStoreFilter') + ->with([$defaultStoreId]) + ->willReturnSelf(); + + $sitemapCollectionMock->expects($this->any()) + ->method('getIterator') + ->willReturn(new \ArrayIterator([$sitemapMockOne, $sitemapMockTwo, $sitemapMockThree])); + + $this->sitemapCollectionFactory->expects($this->once()) + ->method('create') + ->willReturn($sitemapCollectionMock); + + $this->assertEquals($expected, $this->block->toHtml()); + } + + /** + * Check that getIdentities() method returns specified cache tag + */ + public function testGetIdentities() + { + $storeId = 1; + + $this->storeResolver->expects($this->once()) + ->method('getCurrentStoreId') + ->willReturn($storeId); + + $expected = [ + \Magento\Robots\Model\Config\Value::CACHE_TAG . '_' . $storeId, + ]; + $this->assertEquals($expected, $this->block->getIdentities()); + } + + /** + * Initialize mock object of Event Manager + * + * @param string $data + * @return void + */ + protected function initEventManagerMock($data) + { + $this->eventManagerMock->expects($this->any()) + ->method('dispatch') + ->willReturnMap([ + [ + 'view_block_abstract_to_html_before', + [ + 'block' => $this->block, + ], + ], + [ + 'view_block_abstract_to_html_after', + [ + 'block' => $this->block, + 'transport' => new \Magento\Framework\DataObject(['html' => $data]), + ], + ], + ]); + } + + /** + * Create and return mock object of \Magento\Sitemap\Model\Sitemap class + * + * @param string $sitemapPath + * @param string $sitemapFilename + * @return \PHPUnit_Framework_MockObject_MockObject + */ + protected function getSitemapMock($sitemapPath, $sitemapFilename) + { + $sitemapMock = $this->getMockBuilder(\Magento\Sitemap\Model\Sitemap::class) + ->disableOriginalConstructor() + ->setMethods([ + 'getSitemapFilename', + 'getSitemapPath', + 'getSitemapUrl', + ]) + ->getMock(); + + $sitemapMock->expects($this->any()) + ->method('getSitemapFilename') + ->willReturn($sitemapFilename); + $sitemapMock->expects($this->any()) + ->method('getSitemapPath') + ->willReturn($sitemapPath); + $sitemapMock->expects($this->any()) + ->method('getSitemapUrl') + ->with($sitemapPath, $sitemapFilename) + ->willReturn($sitemapFilename); + + return $sitemapMock; + } +} diff --git a/app/code/Magento/Sitemap/Test/Unit/Model/Config/Backend/RobotsTest.php b/app/code/Magento/Sitemap/Test/Unit/Model/Config/Backend/RobotsTest.php new file mode 100644 index 0000000000000..4b58c74334286 --- /dev/null +++ b/app/code/Magento/Sitemap/Test/Unit/Model/Config/Backend/RobotsTest.php @@ -0,0 +1,85 @@ +context = $this->getMockBuilder(\Magento\Framework\Model\Context::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class) + ->getMockForAbstractClass(); + + $this->typeList = $this->getMockBuilder(\Magento\Framework\App\Cache\TypeListInterface::class) + ->getMockForAbstractClass(); + + $this->storeResolver = $this->getMockBuilder(\Magento\Store\Model\StoreResolver::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->model = new \Magento\Sitemap\Model\Config\Backend\Robots( + $this->context, + $this->registry, + $this->scopeConfig, + $this->typeList, + $this->storeResolver + ); + } + + /** + * Check that getIdentities() method returns specified cache tag + */ + public function testGetIdentities() + { + $storeId = 1; + + $this->storeResolver->expects($this->once()) + ->method('getCurrentStoreId') + ->willReturn($storeId); + + $expected = [ + \Magento\Robots\Model\Config\Value::CACHE_TAG . '_' . $storeId, + ]; + $this->assertEquals($expected, $this->model->getIdentities()); + } +} diff --git a/app/code/Magento/Sitemap/composer.json b/app/code/Magento/Sitemap/composer.json index a79c41b5b451b..d5e7b41a72add 100644 --- a/app/code/Magento/Sitemap/composer.json +++ b/app/code/Magento/Sitemap/composer.json @@ -11,7 +11,8 @@ "magento/module-catalog-url-rewrite": "100.2.*", "magento/module-media-storage": "100.2.*", "magento/framework": "100.2.*", - "magento/module-config": "100.2.*" + "magento/module-config": "100.2.*", + "magento/module-robots": "100.2.*" }, "suggest": { "magento/module-config": "100.2.*" diff --git a/app/code/Magento/Sitemap/etc/adminhtml/system.xml b/app/code/Magento/Sitemap/etc/adminhtml/system.xml index 9b29b45db201c..c65311fc5e0d0 100644 --- a/app/code/Magento/Sitemap/etc/adminhtml/system.xml +++ b/app/code/Magento/Sitemap/etc/adminhtml/system.xml @@ -94,6 +94,7 @@ Magento\Config\Model\Config\Source\Yesno + Magento\Sitemap\Model\Config\Backend\Robots diff --git a/app/code/Magento/Sitemap/etc/module.xml b/app/code/Magento/Sitemap/etc/module.xml index d87f24e752602..0edfcf84f644f 100644 --- a/app/code/Magento/Sitemap/etc/module.xml +++ b/app/code/Magento/Sitemap/etc/module.xml @@ -8,6 +8,7 @@ + diff --git a/app/code/Magento/Sitemap/view/frontend/layout/robots_index_index.xml b/app/code/Magento/Sitemap/view/frontend/layout/robots_index_index.xml new file mode 100644 index 0000000000000..801be3074f9a9 --- /dev/null +++ b/app/code/Magento/Sitemap/view/frontend/layout/robots_index_index.xml @@ -0,0 +1,14 @@ + + + + + + + + + diff --git a/app/code/Magento/Tax/Model/Calculation/RateRepository.php b/app/code/Magento/Tax/Model/Calculation/RateRepository.php index 17e57cfec0b18..d9c67ea3ce7b3 100644 --- a/app/code/Magento/Tax/Model/Calculation/RateRepository.php +++ b/app/code/Magento/Tax/Model/Calculation/RateRepository.php @@ -262,7 +262,7 @@ private function validate(\Magento\Tax\Api\Data\TaxRateInterface $taxRate) ); } - if (!\Zend_Validate::is($taxRate->getRate(), 'NotEmpty')) { + if (!\Zend_Validate::is($taxRate->getRate(), 'Float')) { $exception->addError(__('%fieldName is a required field.', ['fieldName' => 'percentage_rate'])); } diff --git a/app/code/Magento/Tax/Test/Unit/Model/Calculation/RateRepositoryTest.php b/app/code/Magento/Tax/Test/Unit/Model/Calculation/RateRepositoryTest.php index c49386767077a..7ed0129de1c28 100644 --- a/app/code/Magento/Tax/Test/Unit/Model/Calculation/RateRepositoryTest.php +++ b/app/code/Magento/Tax/Test/Unit/Model/Calculation/RateRepositoryTest.php @@ -434,4 +434,42 @@ public function testValidate() ); $this->model->save($rateMock); } + + /** + * @expectedException \Magento\Framework\Exception\InputException + * @expectedExceptionMessage percentage_rate is a required field. + */ + public function testValidateWithNoRate() + { + $rateTitles = ['Label 1', 'Label 2']; + + $countryCode = 'US'; + $countryMock = $this->getMock(\Magento\Directory\Model\Country::class, [], [], '', false); + $countryMock->expects($this->any())->method('getId')->will($this->returnValue(1)); + $countryMock->expects($this->any())->method('loadByCode')->with($countryCode)->will($this->returnSelf()); + $this->countryFactoryMock->expects($this->once())->method('create')->will($this->returnValue($countryMock)); + + $regionId = 2; + $regionMock = $this->getMock(\Magento\Directory\Model\Region::class, [], [], '', false); + $regionMock->expects($this->any())->method('getId')->will($this->returnValue($regionId)); + $regionMock->expects($this->any())->method('load')->with($regionId)->will($this->returnSelf()); + $this->regionFactoryMock->expects($this->once())->method('create')->will($this->returnValue($regionMock)); + + $rateMock = $this->getTaxRateMock( + [ + 'id' => null, + 'tax_country_id' => $countryCode, + 'tax_region_id' => $regionId, + 'region_name' => null, + 'tax_postcode' => null, + 'zip_is_range' => true, + 'zip_from' => 90000, + 'zip_to' => 90005, + 'rate' => '', + 'code' => 'Tax Rate Code', + 'titles' => $rateTitles, + ] + ); + $this->model->save($rateMock); + } } diff --git a/app/code/Magento/Theme/Model/Design/Config/DataProvider.php b/app/code/Magento/Theme/Model/Design/Config/DataProvider.php index ef331d836d7f4..08128483bad8f 100644 --- a/app/code/Magento/Theme/Model/Design/Config/DataProvider.php +++ b/app/code/Magento/Theme/Model/Design/Config/DataProvider.php @@ -139,9 +139,50 @@ public function getMeta() } } + if (isset($meta['other_settings']['children']['search_engine_robots']['children'])) { + $meta['other_settings']['children']['search_engine_robots']['children'] = array_merge( + $meta['other_settings']['children']['search_engine_robots']['children'], + $this->getSearchEngineRobotsMetadata( + $scope, + $meta['other_settings']['children']['search_engine_robots']['children'] + ) + ); + } + return $meta; } + /** + * Retrieve modified Search Engine Robots metadata + * + * Disable Search Engine Robots fields in case when current scope is 'stores'. + * + * @param string $scope + * @param array $fields + * @return array + */ + private function getSearchEngineRobotsMetadata($scope, array $fields = []) + { + if ($scope == \Magento\Store\Model\ScopeInterface::SCOPE_STORES) { + $resetToDefaultsData = [ + 'arguments' => [ + 'data' => [ + 'config' => [ + 'disabled' => true, + 'is_disable_inheritance' => true, + ], + ], + ], + ]; + $fields = array_merge($fields, ['reset_to_defaults' => $resetToDefaultsData]); + foreach ($fields as &$field) { + $field['arguments']['data']['config']['disabled'] = true; + $field['arguments']['data']['config']['is_disable_inheritance'] = true; + } + } + return $fields; + } + /** * @deprecated * @return ScopeCodeResolver diff --git a/app/code/Magento/Theme/etc/di.xml b/app/code/Magento/Theme/etc/di.xml index 16d96862c8f7d..4caf504a13d65 100644 --- a/app/code/Magento/Theme/etc/di.xml +++ b/app/code/Magento/Theme/etc/di.xml @@ -219,7 +219,6 @@ design/search_engine_robots/custom_instructions - Magento\Config\Model\Config\Backend\Admin\Robots other_settings/search_engine_robots diff --git a/app/code/Magento/Theme/view/adminhtml/ui_component/design_config_form.xml b/app/code/Magento/Theme/view/adminhtml/ui_component/design_config_form.xml index 20625842a7274..ac734699e4d71 100644 --- a/app/code/Magento/Theme/view/adminhtml/ui_component/design_config_form.xml +++ b/app/code/Magento/Theme/view/adminhtml/ui_component/design_config_form.xml @@ -247,6 +247,7 @@ text default_robots + [WEBSITE]