Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.4KB

  1. <?php
  2. namespace Friendica\Test\src\App;
  3. use Friendica\App;
  4. use Friendica\Core\Config\Configuration;
  5. use Friendica\LegacyModule;
  6. use Friendica\Module\HTTPException\PageNotFound;
  7. use Friendica\Module\WellKnown\HostMeta;
  8. use Friendica\Test\DatabaseTest;
  9. class ModuleTest extends DatabaseTest
  10. {
  11. private function assertModule(array $assert, App\Module $module)
  12. {
  13. $this->assertEquals($assert['isBackend'], $module->isBackend());
  14. $this->assertEquals($assert['name'], $module->getName());
  15. $this->assertEquals($assert['class'], $module->getClassName());
  16. }
  17. /**
  18. * Test the default module mode
  19. */
  20. public function testDefault()
  21. {
  22. $module = new App\Module();
  23. $this->assertModule([
  24. 'isBackend' => false,
  25. 'name' => App\Module::DEFAULT,
  26. 'class' => App\Module::DEFAULT_CLASS,
  27. ], $module);
  28. }
  29. public function dataModuleName()
  30. {
  31. return [
  32. 'default' => [
  33. 'assert' => [
  34. 'isBackend' => false,
  35. 'name' => 'network',
  36. 'class' => App\Module::DEFAULT_CLASS,
  37. ],
  38. 'args' => new App\Arguments('network/data/in',
  39. 'network/data/in',
  40. ['network', 'data', 'in'],
  41. 3),
  42. ],
  43. 'withStrikeAndPoint' => [
  44. 'assert' => [
  45. 'isBackend' => false,
  46. 'name' => 'with_strike_and_point',
  47. 'class' => App\Module::DEFAULT_CLASS,
  48. ],
  49. 'args' => new App\Arguments('with-strike.and-point/data/in',
  50. 'with-strike.and-point/data/in',
  51. ['with-strike.and-point', 'data', 'in'],
  52. 3),
  53. ],
  54. 'withNothing' => [
  55. 'assert' => [
  56. 'isBackend' => false,
  57. 'name' => App\Module::DEFAULT,
  58. 'class' => App\Module::DEFAULT_CLASS,
  59. ],
  60. 'args' => new App\Arguments(),
  61. ],
  62. 'withIndex' => [
  63. 'assert' => [
  64. 'isBackend' => false,
  65. 'name' => App\Module::DEFAULT,
  66. 'class' => App\Module::DEFAULT_CLASS,
  67. ],
  68. 'args' => new App\Arguments(),
  69. ],
  70. 'withBackendMod' => [
  71. 'assert' => [
  72. 'isBackend' => true,
  73. 'name' => App\Module::BACKEND_MODULES[0],
  74. 'class' => App\Module::DEFAULT_CLASS,
  75. ],
  76. 'args' => new App\Arguments(App\Module::BACKEND_MODULES[0] . '/data/in',
  77. App\Module::BACKEND_MODULES[0] . '/data/in',
  78. [App\Module::BACKEND_MODULES[0], 'data', 'in'],
  79. 3),
  80. ],
  81. 'withFirefoxApp' => [
  82. 'assert' => [
  83. 'isBackend' => false,
  84. 'name' => 'login',
  85. 'class' => App\Module::DEFAULT_CLASS,
  86. ],
  87. 'args' => new App\Arguments('users/sign_in',
  88. 'users/sign_in',
  89. ['users', 'sign_in'],
  90. 3),
  91. ],
  92. ];
  93. }
  94. /**
  95. * Test the module name and backend determination
  96. *
  97. * @dataProvider dataModuleName
  98. */
  99. public function testModuleName(array $assert, App\Arguments $args)
  100. {
  101. $module = (new App\Module())->determineModule($args);
  102. $this->assertModule($assert, $module);
  103. }
  104. public function dataModuleClass()
  105. {
  106. return [
  107. 'default' => [
  108. 'assert' => App\Module::DEFAULT_CLASS,
  109. 'name' => App\Module::DEFAULT,
  110. 'command' => App\Module::DEFAULT,
  111. 'privAdd' => false,
  112. ],
  113. 'legacy' => [
  114. 'assert' => LegacyModule::class,
  115. // API is one of the last modules to switch from legacy to new BaseModule
  116. // so this should be a stable test case until we completely switch ;-)
  117. 'name' => 'api',
  118. 'command' => 'api/test/it',
  119. 'privAdd' => false,
  120. ],
  121. 'new' => [
  122. 'assert' => HostMeta::class,
  123. 'not_required',
  124. 'command' => '.well-known/host-meta',
  125. 'privAdd' => false,
  126. ],
  127. '404' => [
  128. 'assert' => PageNotFound::class,
  129. 'name' => 'invalid',
  130. 'command' => 'invalid',
  131. 'privAdd' => false,
  132. ]
  133. ];
  134. }
  135. /**
  136. * Test the determination of the module class
  137. *
  138. * @dataProvider dataModuleClass
  139. */
  140. public function testModuleClass($assert, string $name, string $command, bool $privAdd)
  141. {
  142. $config = \Mockery::mock(Configuration::class);
  143. $config->shouldReceive('get')->with('config', 'private_addons', false)->andReturn($privAdd)->atMost()->once();
  144. $router = (new App\Router([]))->loadRoutes(include __DIR__ . '/../../../static/routes.config.php');
  145. $module = (new App\Module($name))->determineClass(new App\Arguments('', $command), $router, $config);
  146. $this->assertEquals($assert, $module->getClassName());
  147. }
  148. /**
  149. * Test that modules are immutable
  150. */
  151. public function testImmutable()
  152. {
  153. $module = new App\Module();
  154. $moduleNew = $module->determineModule(new App\Arguments());
  155. $this->assertNotSame($moduleNew, $module);
  156. }
  157. }