Commit graph

17 commits

Author SHA1 Message Date
Thiemo Kreuz
b95a07380a Remove meaningless ->expects( $this->any() ) from all tests
It is not entirely meaningless. It might be an indicator that
the number of calls to a method is intentionally unlimited.
This is similar to e.g. an @inheritDoc PHPDoc comment that
marks a method as being "intentionally undocumented".

However, what's the meaning of being "intentionally
unconstrained"? Let's just not have any constraint then.

I feel all these ->expects( $this->any() ) bloat the test
code so much that it's never worth it.

Change-Id: I9925e7706bd03e1666f6eb0b284cb42b0dd3be23
2021-04-23 11:58:58 +02:00
Thiemo Kreuz
f36808dddc Use UserIdentityValue in tests where possible
… instead of PHPUnit mocks. The tiny value class is made for
this.

Change-Id: Ie04cde57126fcafabf8906f9644c6e80345d8a48
2021-04-22 09:32:43 +02:00
Daimona Eaytoy
535d7abf59 phpunit: Mass-replace setMethods with onlyMethods and adjust
Ended up using
  grep -Prl '\->setMethods\(' . | xargs sed -r -i 's/setMethods\(/onlyMethods\(/g'

special-casing setMethods( null ) -> onlyMethods( [] )

and then manual fix of failing test (from PS2 onwards).

Bug: T278010
Change-Id: I012dca7ae774bb430c1c44d50991ba0b633353f1
2021-04-16 20:15:00 +02:00
DannyS712
60a4cc0a81 Remove unused EntryPointTest::$mockHandler
Change-Id: I253cdf9819828ea9ef13c9685fe92c6ed27ce9c4
2021-03-14 20:31:57 +00:00
Petr Pchelko
22c47c4edd Rest: inject Authority into Handler
Bug: T239753
Change-Id: Iddde137c5a86786357d67458f28fe3e4d6e98710
2021-01-21 15:09:34 -06:00
David Barratt
c36b320454
Handle CORS preflight request and prevent anon users from unsafe methods
Creates an OPTIONS handler that handles any OPTIONS requests that are
not already handled by a handler. CORS has no mechanism to ensure the
user is authenticated, so the Router will reject cross-origin requests
from anon users.

This change allows authenticated users to make cross-origin
requests if they authenticate with OAuth or if
$wgRestAllowCrossOriginCookieAuth is enabled.

Bug: T232176
Bug: T262712
Change-Id: I128b4bdbec4f6bea35142153c951fd7b79617106
2020-09-21 19:29:40 -04:00
David Barratt
ab06b05619
Add option to enable cross-origin resource sharing (CORS) in REST API
When MediaWiki is not behind an intranet, it is completely safe to
add the Access-Control-Allow-Origin: * header to responses and allow
cross-origin sites to access the REST API.

Bug: T232176
Change-Id: Ic0658039a6a46ee4f50c76f5d100450fdef7525a
2020-08-04 15:49:58 -04:00
addshore
959bc315f2 MediaWikiTestCase to MediaWikiIntegrationTestCase
The name change happened some time ago, and I think its
about time to start using the name name!
(Done with a find and replace)

My personal motivation for doing this is that I have started
trying out vscode as an IDE for mediawiki development, and
right now it doesn't appear to handle php aliases very well
or at all.

Change-Id: I412235d91ae26e4c1c6a62e0dbb7e7cf3c5ed4a6
2020-06-30 17:02:22 +01:00
Tim Starling
68c433bd23 Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.

General principles:
* Use DI if it is already used. We're not changing the way state is
  managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
  is a service, it's a more generic interface, it is the only
  thing that provides isRegistered() which is needed in some cases,
  and a HookRunner can be efficiently constructed from it
  (confirmed by benchmark). Because HookContainer is needed
  for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
  SpecialPage and ApiBase have getHookContainer() and getHookRunner()
  methods in the base class, and classes that extend that base class
  are not expected to know or care where the base class gets its
  HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
  getHookRunner() methods, getting them from the global service
  container. The point of this is to ease migration to DI by ensuring
  that call sites ask their local friendly base class rather than
  getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
  methods did not seem warranted, there is a private HookRunner property
  which is accessed directly. Very rarely (two cases), there is a
  protected property, for consistency with code that conventionally
  assumes protected=private, but in cases where the class might actually
  be overridden, a protected accessor is preferred over a protected
  property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
  global code. In a few cases it was used for objects with broken
  construction schemes, out of horror or laziness.

Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore

Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router

setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine

Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-05-30 14:23:28 +00:00
daniel
65342f8353 Define POST handler for /page/: create page
NOTE: once this is merged, also merge Ie7b47e6868cc on the OAuth repo,
to fix unit tests after a breaking change to Router's constructor
signature.

Bug: T230842
Change-Id: I8f5b92918a58e44a4f2d8c78d234d9f64c2d06bf
2020-03-25 20:49:20 +01:00
James D. Forrester
4f2d1efdda Coding style: Auto-fix MediaWiki.Classes.UnsortedUseStatements.UnsortedUse
Change-Id: I94a0ae83c65e8ee419bbd1ae1e86ab21ed4d8210
2020-01-10 09:32:25 -08:00
Petr Pchelko
0df763f71d Use UserIdentity instead of User in REST
Change-Id: Ia6a517c6a64664be2363492108f9497fc949f299
2019-09-18 14:45:01 +10:00
Tim Starling
8b1a6cc58a Use TextFormatter in the REST API
* Add ResponseFactory::createLocalizedHttpError(), which generates a
  JSON response body from a MessageValue
* ResponseFactory::__construct() accepts an array of TextFormatter
  objects. For ease of testing, the array may be empty. The integrated
  ResponseFactory has a TextFormatter for English, and one for
  $wgContLang if that is different.
* Use createLocalizedHttpError() to show helpful error messages for
  errors generated by Router.

Change-Id: I897a0aee42227916c568333ab384966f1b87f599
2019-09-17 16:03:14 +10:00
Brad Jorsch
ebfbd2d42a rest: Use ParamValidator library, add BodyValidator
Parameter validation is based on parameter definitions like those in the
Action API, using the new ParamValidator library. Handlers should use
the provided Handler methods to access parameters rather than fetching
them directly from the RequestInterface.

Body validation allows the handler to have the (non-form-data) body of a
request parsed and validated. The only validator included in this patch
ignores the body entirely; future patches may implement validation for
JSON bodies based on JSON schemas, or the like.

Bug: T223239
Change-Id: I3c37ea2b432840514b6bff90007c8403989225d5
2019-09-04 10:12:35 -04:00
Tim Starling
86693df247 REST: call MediaWiki::preOutputCommit and doPostOutputShutdown
As in api.php. Among other things, this enables profiling.

Move EntryPoint test out of unit/ so that it passes.

Use ob_start()/ob_end_clean() instead of assuming an output buffer is
open, so that EntryPoint::execute() can be run from CLI mode.

Change-Id: I38162a9eac6fd5acfed2035b87cac4a97ffd50d6
2019-08-26 14:19:43 +10:00
Amir Sarabadani
095f5583cb Move trivial unit tests, round II
Change-Id: I18e5a1514d7372b34f7fb460adf506a1ac65001f
2019-07-02 19:52:29 +02:00
Tim Starling
4e0e36397c REST: Testable EntryPoint
* Split EntryPoint into a static main() and a non-static execute()
* Add tests for execute()

Change-Id: I025356b04ddc5a16494f98c446d785d6bb05ab10
2019-06-14 17:01:15 +10:00