StatusTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Module\Test\Unit;
  7. use \Magento\Framework\Module\Status;
  8. use Magento\Framework\Config\File\ConfigFilePool;
  9. class StatusTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \PHPUnit_Framework_MockObject_MockObject
  13. */
  14. private $loader;
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $moduleList;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $writer;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $conflictChecker;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $dependencyChecker;
  31. /**
  32. * @var Status
  33. */
  34. private $object;
  35. protected function setUp()
  36. {
  37. $this->loader = $this->createMock(\Magento\Framework\Module\ModuleList\Loader::class);
  38. $this->moduleList = $this->createMock(\Magento\Framework\Module\ModuleList::class);
  39. $this->writer = $this->createMock(\Magento\Framework\App\DeploymentConfig\Writer::class);
  40. $this->conflictChecker = $this->createMock(\Magento\Framework\Module\ConflictChecker::class);
  41. $this->dependencyChecker = $this->createMock(\Magento\Framework\Module\DependencyChecker::class);
  42. $this->object = new Status(
  43. $this->loader,
  44. $this->moduleList,
  45. $this->writer,
  46. $this->conflictChecker,
  47. $this->dependencyChecker
  48. );
  49. }
  50. public function testCheckConstraintsEnableAllowed()
  51. {
  52. $this->conflictChecker->expects($this->once())
  53. ->method('checkConflictsWhenEnableModules')
  54. ->will($this->returnValue(['Module_Foo' => [], 'Module_Bar' => []]));
  55. $this->dependencyChecker->expects($this->once())
  56. ->method('checkDependenciesWhenEnableModules')
  57. ->will($this->returnValue(['Module_Foo' => [], 'Module_Bar' => []]));
  58. $result = $this->object->checkConstraints(
  59. true,
  60. ['Module_Foo' => '', 'Module_Bar' => ''],
  61. ['Module_baz', 'Module_quz']
  62. );
  63. $this->assertEquals([], $result);
  64. }
  65. public function testCheckConstraintsEnableNotAllowed()
  66. {
  67. $this->conflictChecker->expects($this->once())
  68. ->method('checkConflictsWhenEnableModules')
  69. ->will($this->returnValue(['Module_Foo' => ['Module_Bar'], 'Module_Bar' => ['Module_Foo']]));
  70. $this->dependencyChecker->expects($this->once())
  71. ->method('checkDependenciesWhenEnableModules')
  72. ->will($this->returnValue(
  73. [
  74. 'Module_Foo' => ['Module_Baz' => ['Module_Foo', 'Module_Baz']],
  75. 'Module_Bar' => ['Module_Baz' => ['Module_Bar', 'Module_Baz']],
  76. ]
  77. ));
  78. $result = $this->object->checkConstraints(true, ['Module_Foo' => '', 'Module_Bar' => ''], [], false);
  79. $expect = [
  80. 'Cannot enable Module_Foo because it depends on disabled modules:',
  81. "Module_Baz: Module_Foo->Module_Baz",
  82. 'Cannot enable Module_Bar because it depends on disabled modules:',
  83. "Module_Baz: Module_Bar->Module_Baz",
  84. 'Cannot enable Module_Foo because it conflicts with other modules:',
  85. "Module_Bar",
  86. 'Cannot enable Module_Bar because it conflicts with other modules:',
  87. "Module_Foo",
  88. ];
  89. $this->assertEquals($expect, $result);
  90. }
  91. public function testCheckConstraintsEnableNotAllowedWithPrettyMsg()
  92. {
  93. $this->conflictChecker->expects($this->once())
  94. ->method('checkConflictsWhenEnableModules')
  95. ->will($this->returnValue(['Module_Foo' => ['Module_Bar'], 'Module_Bar' => ['Module_Foo']]));
  96. $this->dependencyChecker->expects($this->once())
  97. ->method('checkDependenciesWhenEnableModules')
  98. ->will($this->returnValue(
  99. [
  100. 'Module_Foo' => ['Module_Baz' => ['Module_Foo', 'Module_Baz']],
  101. 'Module_Bar' => ['Module_Baz' => ['Module_Bar', 'Module_Baz']],
  102. ]
  103. ));
  104. $result = $this->object->checkConstraints(true, ['Module_Foo' => '', 'Module_Bar' => ''], [], true);
  105. $expect = [
  106. 'Cannot enable Module_Foo',
  107. 'Cannot enable Module_Bar',
  108. 'Cannot enable Module_Foo because it conflicts with other modules:',
  109. "Module_Bar",
  110. 'Cannot enable Module_Bar because it conflicts with other modules:',
  111. "Module_Foo",
  112. ];
  113. $this->assertEquals($expect, $result);
  114. }
  115. public function testCheckConstraintsDisableAllowed()
  116. {
  117. $this->dependencyChecker->expects($this->once())
  118. ->method('checkDependenciesWhenDisableModules')
  119. ->will($this->returnValue(['Module_Foo' => [], 'Module_Bar' => []]));
  120. $result = $this->object->checkConstraints(false, ['Module_Foo' => '', 'Module_Bar' => '']);
  121. $this->assertEquals([], $result);
  122. }
  123. public function testCheckConstraintsDisableNotAllowed()
  124. {
  125. $this->dependencyChecker->expects($this->once())
  126. ->method('checkDependenciesWhenDisableModules')
  127. ->will($this->returnValue(
  128. [
  129. 'Module_Foo' => ['Module_Baz' => ['Module_Baz', 'Module_Foo']],
  130. 'Module_Bar' => ['Module_Baz' => ['Module_Baz', 'Module_Bar']],
  131. ]
  132. ));
  133. $result = $this->object->checkConstraints(false, ['Module_Foo' => '', 'Module_Bar' => '']);
  134. $expect = [
  135. 'Cannot disable Module_Foo because modules depend on it:',
  136. "Module_Baz: Module_Baz->Module_Foo",
  137. 'Cannot disable Module_Bar because modules depend on it:',
  138. "Module_Baz: Module_Baz->Module_Bar",
  139. ];
  140. $this->assertEquals($expect, $result);
  141. }
  142. public function testSetIsEnabled()
  143. {
  144. $modules = ['Module_Foo' => '', 'Module_Bar' => '', 'Module_Baz' => ''];
  145. $this->loader->expects($this->once())->method('load')->willReturn($modules);
  146. $this->moduleList->expects($this->at(0))->method('has')->with('Module_Foo')->willReturn(false);
  147. $this->moduleList->expects($this->at(1))->method('has')->with('Module_Bar')->willReturn(false);
  148. $this->moduleList->expects($this->at(2))->method('has')->with('Module_Baz')->willReturn(false);
  149. $expectedModules = ['Module_Foo' => 1, 'Module_Bar' => 1, 'Module_Baz' => 0];
  150. $this->writer->expects($this->once())->method('saveConfig')
  151. ->with([ConfigFilePool::APP_CONFIG => ['modules' => $expectedModules]]);
  152. $this->object->setIsEnabled(true, ['Module_Foo', 'Module_Bar']);
  153. }
  154. /**
  155. * @expectedException \LogicException
  156. * @expectedExceptionMessage Unknown module(s): 'Module_Baz'
  157. */
  158. public function testSetIsEnabledUnknown()
  159. {
  160. $modules = ['Module_Foo' => '', 'Module_Bar' => ''];
  161. $this->loader->expects($this->once())->method('load')->willReturn($modules);
  162. $this->object->setIsEnabled(true, ['Module_Baz']);
  163. }
  164. /**
  165. * @dataProvider getModulesToChangeDataProvider
  166. * @param bool $firstEnabled
  167. * @param bool $secondEnabled
  168. * @param bool $thirdEnabled
  169. * @param bool $isEnabled
  170. * @param string[] $expected
  171. */
  172. public function testGetModulesToChange($firstEnabled, $secondEnabled, $thirdEnabled, $isEnabled, $expected)
  173. {
  174. $modules = ['Module_Foo' => '', 'Module_Bar' => '', 'Module_Baz' => ''];
  175. $this->loader->expects($this->once())->method('load')->willReturn($modules);
  176. $this->moduleList->expects($this->at(0))->method('has')->with('Module_Foo')->willReturn($firstEnabled);
  177. $this->moduleList->expects($this->at(1))->method('has')->with('Module_Bar')->willReturn($secondEnabled);
  178. $this->moduleList->expects($this->at(2))->method('has')->with('Module_Baz')->willReturn($thirdEnabled);
  179. $result = $this->object->getModulesToChange($isEnabled, ['Module_Foo', 'Module_Bar', 'Module_Baz']);
  180. $this->assertEquals($expected, $result);
  181. }
  182. /**
  183. * @return array
  184. */
  185. public function getModulesToChangeDataProvider()
  186. {
  187. return [
  188. [true, true, true, true, []],
  189. [true, true, false, true, ['Module_Baz']],
  190. [true, false, true, true, ['Module_Bar']],
  191. [true, false, false, true, ['Module_Bar', 'Module_Baz']],
  192. [false, false, false, true, ['Module_Foo', 'Module_Bar', 'Module_Baz']],
  193. [true, false, false, false, ['Module_Foo']],
  194. [false, true, false, false, ['Module_Bar']],
  195. [false, true, true, false, ['Module_Bar', 'Module_Baz']],
  196. [true, true, true, false, ['Module_Foo', 'Module_Bar', 'Module_Baz']],
  197. ];
  198. }
  199. }