aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/unit/includes/Rest/Handler/HandlerTestTrait.php
Commit message (Collapse)AuthorAgeFilesLines
* REST: Make OpenAPI spec info strings translatablebpirkle2025-03-211-0/+6
| | | | | | | | | OpenAPI specs include an "info" section that includes strings such as "title" and "description" that are intended to be human-readable. Make all such strings translatable. Bug: T385855 Change-Id: I15285be6d196c0e7fd7e922f23058d7c09b6b31a
* Use explicit nullable type on parameter argumentsUmherirrender2024-10-161-7/+7
| | | | | | | | | | | Implicitly marking parameter $... as nullable is deprecated in php8.4, the explicit nullable type must be used instead Created with autofix from Ide15839e98a6229c22584d1c1c88c690982e1d7a Break one long line in SpecialPage.php Bug: T376276 Change-Id: I807257b2ba1ab2744ab74d9572c9c3d3ac2a968e
* REST: add restbase compat error handling modebpirkle2024-09-161-1/+7
| | | | | | | | | | | | In order to replace certain /api/rest_v1 endpoints, we need to have something in MediaWiki that generates a compatible responser. The MediaWiki REST API error format does not match RESTbase. So the easiest approach seemed to be to add a compatibility mode, triggered using the x-restbase-compat header. This can be set via the gateway when rerouting the request. Bug: T374136 Change-Id: I73934940d367be52941bd27861c248ab5bcfb5d2
* REST: Make module definition files more like OpenAPI specsdaniel2024-06-241-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | This splits RouteFileModule into two classes, ExtraRoutesModule and SpecBasedModule. ExtraRoutesModule has no module prefix and supports only "flat" route definition files and additional routes from extension.json. SpecBasedModule represents a single module defined in a definition file similar to an OpenAPI spec. The idea is that a full OpenAPI spec can be generated by filling in any missing information based on information provided by the Handler implementation. In particular, the definition of parameters and request body schemas will be generated. A JSON schema for the new file format is added under docs/rest/. Support for the intermediate format introduced in Iebcde4645d4 is removed. It was not included in a release and was not being used outside core tests. Bug: T366837 Change-Id: I4ce306b0997f80b78a3d901e38bbfa8445bed604
* REST: HandlerTestTrait should mock validateBodyParams instead of validateBody.daniel2024-05-281-2/+11
| | | | | | | | The validateBody method in validator is based on BodyValidator. Returning a non-null value from it triggers b/c behavior in Handler::validate. That should generally be avoided. Change-Id: I3047cb0b6fdd8ed623b43910e737d8882f333ff6
* Remove all @package commentsthiemowmde2024-05-101-1/+0
| | | | | | | | | | | | I don't think these do anything with the documentation generators we currently use. Especially not in tests. How are tests part of a "package" when the code is not? Note how most of these are simply identical to the namespace. They are most probably auto-generated by some IDEs but don't actually mean anything. Change-Id: I771b5f2041a8e3b077865c79cbebddbe028543d1
* Introduce Modules into the REST frameworkdaniel2024-05-081-31/+78
| | | | | | | Modules group together endpoints by a shared prefix. The idea is that each module has its own version and can generated self-contained self-documentation. This allows clients to have clear expectations about the endpoints of each module, no matter what wiki they are accessing. So far, each wiki may be exposing a different set of endpoints, with no way to provide a spec that describes that set of endpoints in a way that would be consistent across wikis and stable over time. Bug: T362480 Change-Id: Iebcde4645d472d27eee5a30adb6eee12cc7d046b
* Tests: enable non-integration tests to use TitleFactory service.daniel2024-04-251-3/+1
| | | | | | | | | | | | | | | | | | | | This change implements two things that can be combined to allow the usage of services like the TitleFactory in subclasses of MediaWikiUnitTestCase: 1) This adds getServiceContainer() to the MediaWikiUnitTestCase base class, to mirror the method of the same name on MediaWikiIntegrationtest case, along with a a setService() method for defining service objects for use in tests. 2) This adds makeMockTitleFactory() to the MockTitleTrait, to allow tests to esaily define the TitleFactory service. Making services available through a service container primarily benefits code that relies on ObjectFactory, like REST handlers. Needed-By: Iac113a9e766e38b2d19ae99b7e448548a515469e Change-Id: Ida6c37c7133b9e3887268025daf6e5b7c1340e52
* REST: HandlerTestTrait should set parsed body datadaniel2024-03-121-0/+7
| | | | | | | | | | HandlerTestTrait::initHandler needs to emulate the behavior of Router::setBodyData, so test cases that provide an unparsed body in a RequestData object don't break when we migrate handlers away from using BodyValidator. Bug: T358850 Depends-On: Icebf044dd4668b36bc9ba76f11520b7acc3347c0 Change-Id: Ib64dbfa4b76fd5c1cc575c835f63347581cc6a9d
* Merge "DummyServicesTrait: add an ITextFormatter"jenkins-bot2024-01-121-11/+3
|\
| * DummyServicesTrait: add an ITextFormatterDannyS7122024-01-091-11/+3
| | | | | | | | | | | | To avoid duplicating the creation of mocks/implementations Change-Id: Id1094a9009acd100092f1b7cd55016d78900ce5f
* | Add phpunit test for RootSpecHandlerdaniel2023-12-061-18/+26
|/ | | | | | Bug: T352664 Change-Id: I9dd885b87d3c09f2d67bb707300741358f503017 Co-authored-by: Atieno <pnjira@wikimedia.org>
* REST: support relative redirectsdaniel2023-10-191-6/+13
| | | | | | | | | | | | | Make HTTP redirects returned by the REST API use relative URLs in the Location header. This ensures interoperability with the WMF service mesh, avoiding issues with internal services trying to resolve redirects by accessing the canonical external URLs. Note that relative URLs are allowed in the Location header per RFC 7231, while the obsolete RFC 2616 required it to be absolute. Bug: T349001 Change-Id: Ibb38c30eb66aaed14ab3ad01ffc42c348a0d07c8
* HandlerTestTrait: Always mock validateParams and validateBodyKosta Harlan2023-09-051-6/+2
| | | | | | | | | | | | | | | | | | | | Why: - An empty array is a valid return value for the method, and... - Without this, tests that use `executeHandler()` with an empty parameters array (for example, the REST endpoint has no query parameters) and items in the body params array will result in a `getValidatedParams() called before validate()` exception in `getValidatedParams`, because the Handler's `validatedParams` property will be null instead of an empty array What: - Remove the truthiness check for query and body params, and always set the array (empty or otherwise) as the return value for `validateParams` and `validateBody` Change-Id: I9cf36775b7392734b184b63023beef4a2a8434ac
* Use native assertGreater/LessThan in HandlerTestTraitthiemowmde2023-07-031-4/+2
| | | | | | | The main benefit is that we can see the actual value, instead of "expected true". Change-Id: Ib620348911a9612aa630cb331b2b23b66207d57b
* Make it possible to override the session in REST API testsDaimona Eaytoy2023-02-061-12/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The current signature of the various execute methods only takes a boolean parameter to determine if the session should be safe against CSRF, but that does not give callers fine-grained control over the Session object, including setting a specific token. Also, do not use createNoOpMock in getSession(), since it implies strong assertions on what methods are called. This way, getSession can also be used to get a simple mock session that tests may further manipulate. Make $csrfSafe parameter of SessionHelperTestTrait::getSession mandatory. This way, callers are forced to think what makes sense in each use case. The various methods in HandlerTestTrait now default to a session that is safe against CSRF. This assumes that most REST handlers don't care about the session, and that any handler that does care about the session and where someone needs to test the behaviour in case of bad/missing token will explicitly provide a Session that is NOT safe against CSRF. Typehint the return value of Session(Backend)::getUser so that PHPUnit will automatically make it return a mock User object even if the method is not explicitly mocked. Remove a useless PHPUnit assertion -- setting the return value to be X and then veryfing that is equal to X is a tautology, and can only fail if the test itself is flawed (as was the case, since it was using stdClass as the return type for all methods). Remove the getUser test case altogether, there's no way to make it work given the DummySessionBackend, and the test isn't that helpful anyway. More and more methods will have the same issue as soon as their return value is typehinted. Follow-up: I2a9215bf909b83564247ded95ecdb4ead0615150 Change-Id: Ic51dc3e7bf47c81f2ac4705308bb9ecd8275bbaf
* Follow redirects for page/{title} formats source/baremsantos2022-12-021-0/+1
| | | | | | | | | | | | | | | | | | * Share logic previously implemented for html/with formats through a trait class * source/bare formats doesn't execute a temporary redirect. the JSON body will contain a key "redirect_target" instead if a wiki redirect is found * Introduce PageRedirectHandlerTest to test redirect logic shared between multiple handlers * Move Handler instatiation to HandlerTestTrait * Update api-testing tests in Update.js Change-Id: Id66e33e19adabdb3c9621eaea4a5d441f23edafd
* Fix incomplete ITextFormatter mocksthiemowmde2022-11-211-4/+9
| | | | | | | | | Otherwise the mocked getLangCode() method returns null, which is not allowed any more in PHP 8.1. Bug: T289926 Required-For: I7e026cca216aba24ee5d5662b6fca322b3cec9ae Change-Id: I178def7f03a44f6b49cdb461d9ab340e1c89517f
* Introduce SearchResultThumbnailProvider & move hook + NS_FILE thumbs inMatthias Mullie2022-10-031-5/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | What was previously a REST API-only feature (the thumbnails hook allowing for thumbnails for non-file pages via the PageImages extension) is now also being adopted in the main search page. That hook will now be called with NS_FILE result thumbnails pre-filled, which was not the case previously. PageImages essentially duplicated NS_FILE thumbnail logic that was already present in Special:Search, so that can (and will in a follow-up patch) then be removed there. Special:Search will then simply take whatever is produced from the provider (which will include both NS_FILE thumbs - which it handled already - as well as whatever else it receives from the hook), as will the REST API (which already received both) Since thumbnails can now come in for multiple namespaces & having some of those results with & others without a thumbnail can be quite jarring, it was decided that we'd display placeholder images (for certain namespaces). This is now controlled by $wgThumbnailNamespaces. I also split up a few things in FullSearchResultWidget:: generateFileHtml for more clarity. Meanwhile also updated mediawiki.special.search.styles.less to use variables for known colors. Also implemented a 'transform' (required for testing this change properly) and 'getDisplayWidthHeight' (it became needed after implementing transform) callback function for mock Files, and updated some existing tests in response to these changes. And some more Rest test files have been updated to allow passing around a HookContainer instead of only an array of hooks (from which a new HookContainer would then be created) to allow the same container to be used across all relevant objects, who may have it injected as dependency. Bug: T306883 Change-Id: I2a679b51758020d3e822da01a1bde1ae632b0b0a
* Make HandlerTestTrait compatible with php8.1Brian Wolff2022-07-231-1/+1
| | | | | | | | | Cast values to string. This seems to be consistent with what Router::substPathParams does, and also would be consistent with the pre php-8.1 behaviour. Bug: T313663 Change-Id: I631478101b7a679efa7bb897c6c2bb5d51d6281b
* Allow REST API handlers to require csrf-safe session providersBill Pirkle2022-05-201-6/+15
| | | | | | Bug: T305043 Depends-On: Ic7c1b19b86e8a151e2d42aaec00ef0e89db77f08 Change-Id: Ic6bd48b400ecd839ef99b518ef955781470cd05c
* Use updated ObjectFactory namespaceAlexander Vorwerk2022-03-091-1/+1
| | | | | | | Depends-On: I99c5e5664d2401c36a9890f148eba7c25e6e8324 Depends-On: I48ab818b2965da14af15ef370aa83ad9455badd9 Depends-On: I018371e4b77911e56152ca7b2df734afc73f58a5 Change-Id: I04ebdb52102f6191d49a9cc70b1f98308299e72f
* phpunit: Don't redefine methods in traitsDaimona Eaytoy2021-03-231-9/+0
| | | | | | | | | | | | | | | The signatures of these methods will change in PHPUnit 9, and in general, having these methods means additional code to maintain. Since this is test code, any attempt to use these traits from a class which doesn't have the required methods will fail hard, and as such, there's no reason to have these methods at all. Also, MockServiceDependenciesTrait was redeclaring fail() as a non-static method, but it is actually static in the parent class, which makes tests fail on PHP 8. Bug: T243600 Change-Id: I78bf1bdd2eb2e09aed27d1b6bff2302e28600703
* Tests: Mark more more closures as staticUmherirrender2021-02-091-2/+2
| | | | | | | Result of a new sniff I25a17fb22b6b669e817317a0f45051ae9c608208 Bug: T274036 Change-Id: I695873737167a75f0d94901fa40383a33984ca55
* Introduce MockAuthorityTrait for Authority test helpersPetr Pchelko2021-01-211-40/+7
| | | | Change-Id: I152f2cbe8a7000eedc237fb46b4e4c6d2f9730d4
* Rest: use Authority in all core handlersPetr Pchelko2021-01-211-15/+25
| | | | | Bug: T239753 Change-Id: Idf2229255f49514dd8b68bf63573c5b619b4f2f1
* Rest: inject Authority into HandlerPetr Pchelko2021-01-211-34/+24
| | | | | Bug: T239753 Change-Id: Iddde137c5a86786357d67458f28fe3e4d6e98710
* Inject $user for validation instead of creating it inside TraitPeter Ovchyn2021-01-201-8/+33
| | | | | Bug: T264058 Change-Id: I74a8d08f8a867c6942dfe21ac875bec1ebd85422
* Split title mocking methods out of HandlerTestTrait.Petr Pchelko2021-01-131-78/+5
| | | | | | | | The functionality of creating title mocks is generally useful and this will also allow to make HandlerTestTrait more narrow. Bug: T264058 Change-Id: I76eca48dfcff65a6203fccde5366912a2d66c495
* Use UserDef in UserContribs endpointsNikki Nikkhoui2020-08-051-23/+50
| | | | Change-Id: I9620e45ab1aa28dfa526705fce163ed5a27c4f86
* HandlerTestTrait::getRouteUrl behaving differently than Router::getRouteUrlNikki Nikkhoui2020-06-161-1/+4
| | | | | | | | | | | In HandlerTestTrait::getRouteUrl when a query param had a null value, the query was not being appended to the url at all. Whereas, in Router::getRouteUrl a null value would still append on the query key. HandlerTestTrait::getRouteUrl will now append on query params, even if the value is null. Additionally, Router::getRouteUrl() can accept path params. Bug: T255582 Change-Id: I3610f6252f2f0e7ec95ca346a6bdcd774e5260f8
* REST /me/contributions: allow paging forwardNikki Nikkhoui2020-06-121-0/+3
| | | | | | | | | | | This is part of a chain of patches that implement the user contributions endpoint. In this patch, we add the ability to page through changes, from latest to oldest only. Bug: T252202 Change-Id: If1e862e419ce86b68606ddf28a78304461d3a6d8
* REST me/contributions: Returns 401 UnauthorizedNikki Nikkhoui2020-06-091-1/+4
| | | | | | | | | | | | | This is the first in a chain of patches that iteratively create the me/contributions endpoint that returns a list of contributions by the currently logged in user. This patch contains stubs for major classes needed as well as implementation and test just for returning a 401 Unauthorized for anonymous users. Bug:T252202 Change-Id: Ib75711bb015f476e9d486cb3008eb7b06b148d00
* Hooks::run() call site migrationTim Starling2020-05-301-8/+20
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* REST Handler: add unit tests for base classdaniel2020-05-261-13/+34
| | | | | | | | | | This adds unit tests for the Handler base class. This includes tests for public and protected methods. Since the Handler base class is an extension point, the protected methods are part of the stable public interface. Change-Id: Ibe9fcbb139683dad03b92cd0618c3c0e8feb8b94
* Merge "REST endpoints: Add etag and last-modified headers"jenkins-bot2020-03-261-7/+89
|\
| * REST endpoints: Add etag and last-modified headersdaniel2020-03-261-7/+89
| | | | | | | | | | | | | | | | | | | | | | | | Modified endpoints: * /v1/page/{title}/history * /v1/revision/{id}/bare * /coredev/v0/page/{title}/links/language * /coredev/v0/page/{title}/links/media * /coredev/v0/file/{title} Bug: T238374 Change-Id: I3ddf8fafc17eba3aec724a445d38af98bbc74dc1
* | Define POST handler for /page/: create pagedaniel2020-03-251-2/+5
|/ | | | | | | | | 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
* page/update endpointdaniel2020-03-231-0/+25
| | | | | Bug: T230843 Change-Id: I95289eddfc9ab2e0cef11b9363a5e239cdb2258e
* SearchHandler: add unit testdaniel2020-03-021-0/+134
Change-Id: I963a2f31afddd37ada615f12d6d5725c20a18631