Jump to content

Module:Mock title/doc: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
document MockTitle function
m MockTitle: fix missing space
Line 85: Line 85:
* '''fileWidth''' - the file width in pixels (not used for non-file titles). (int, optional)
* '''fileWidth''' - the file width in pixels (not used for non-file titles). (int, optional)
* '''fileHeight''' - the file height in pixels (not used for non-file titles). (int, optional)
* '''fileHeight''' - the file height in pixels (not used for non-file titles). (int, optional)
* '''filePages''' - pages in the file (not used for non-file titles). See [[mw:Extension:Scribunto/Lua reference manual#File metadata|the Scribunto documentation]] of the title object <code>pages</code> parameterfor the correct format. (table, optional)
* '''filePages''' - pages in the file (not used for non-file titles). See [[mw:Extension:Scribunto/Lua reference manual#File metadata|the Scribunto documentation]] of the title object <code>pages</code> parameter for the correct format. (table, optional)
* '''fileSize''' - the file size in bytes (not used for non-file titles). (int, optional)
* '''fileSize''' - the file size in bytes (not used for non-file titles). (int, optional)
* '''fileMimeType''' - the [[MIME type]] of the file (not used for non-file titles). (int, optional)
* '''fileMimeType''' - the [[MIME type]] of the file (not used for non-file titles). (int, optional)

Revision as of 23:16, 16 August 2023

This module allows you to easily mock Scribunto title objects. This can be useful when writing unit tests for modules which depend on whether a certain page exists, or whether a certain page is a redirect, etc. If you don't use mock title objects, then you run the risk of some editor changing a page that your test cases relied on (e.g. redirecting it or deleting it), thus breaking your tests. By using mock objects, you get to specify things like existence status and redirect status in your tests, making them more resilient against changes to real pages.

Usage

Loading the module

Load the module using require:

local mMockTitle = require('Module:Mock title')

Registering titles to mock

You can register a title to be mocked with the registerMockTitle function.

mMockTitle.registerMockTitle({title = "Main Page", isRedirect = true, redirectTarget = "Wikipedia:Village stocks"})

Or you can register multiple titles at once using registerMockTitles.

mMockTitle.registerMockTitles(
    {title = "File:Example.png", height = 250, width = 200},
    {title = "Template:3x", editProtection = "templateeditor"}
)

You can also register a mock for the current page with registerMockCurrentTitle.

mMockTitle.registerMockCurrentTitle({title = "User:Example"})

Patching title constructors

During tests, you can use the patchTitleConstructors function to temporarily replace the title constructors mw.title.new, mw.title.makeTitle and mw.title.getCurrentTitle with functions that return the mock titles that you registered. You pass patchTitleConstructors your own function containing your test code; while the function is running the title constructors will return mock titles, but after it finishes running they will be restored to their original versions.

local function logCurrentTitle()
    mw.log(mw.title.getCurrentTitle().prefixedText)
end

logCurrentTitle() -- Logs "Module:Mock title"
mMockTitle.patchTitleConstructors(logCurrentTitle) -- Logs "User:Example"
logCurrentTitle() -- Logs "Module:Mock title"

API documentation

MockTitle

Creates a mock title object.

Mock title objects function like normal title objects, but values you specify when creating them are used instead of the real values taken from the wiki.

Usage:

mMockTitle.MockTitle(options)

Parameters:

This function takes a table of options. The possible keys are as follows:

  • title - the page title. If this is a string, it is the page title as used with mw.title.new. If it is a number, it is the page ID. (required)
  • id - the page ID. (int, optional)
  • contentModel - the content model. (string, optional)
  • exists - whether the page exists. (bool, optional)
  • isRedirect - whether the page is a redirect. (bool, optional)
  • redirectTarget - the redirect target of the page. This can be a string (the page title as used with mw.title.new), a number (the page ID), a table of options for MockTitle, or a title object (mock or otherwise).
  • content - the page's content. (string, optional)
  • editProtection - the edit protection level, e.g. "sysop" or "editsemiprotected". (string, optional)
  • moveProtection - the move protection level, e.g. "sysop" or "extendedconfirmed". (string, optional)
  • createProtection - the create protection level, e.g. "sysop". (string, optional)
  • uploadProtection - the upload protection level, e.g. "sysop". (string, optional)
  • cascadingEditProtection - the edit protection level for cascading protection, e.g. "sysop" or "editsemiprotected". (string, optional)
  • cascadingMoveProtection - the move protection level for cascading protection, e.g. "sysop" or "extendedconfirmed". (string, optional)
  • cascadingCreateProtection - the create protection level for cascading protection, e.g. "sysop". (string, optional)
  • cascadingUploadProtection - the upload protection level for cascading protection, e.g. "sysop". (string, optional)
  • fileExists - whether the file exists (not used for non-file titles). (bool, optional)
  • fileWidth - the file width in pixels (not used for non-file titles). (int, optional)
  • fileHeight - the file height in pixels (not used for non-file titles). (int, optional)
  • filePages - pages in the file (not used for non-file titles). See the Scribunto documentation of the title object pages parameter for the correct format. (table, optional)
  • fileSize - the file size in bytes (not used for non-file titles). (int, optional)
  • fileMimeType - the MIME type of the file (not used for non-file titles). (int, optional)
  • fileLength - the file length (duration) in seconds (not used for non-file titles). (int, optional)

Example 1: create a mock title of a fully protected file.

local protectedFileMock = mMockTitle.MockTitle{
    title = "File:Example.png",
    editProtection = "sysop",
    moveProtection = "sysop",
}

Example 2: create a mock circular redirect.

local circularRedirectMock = mMockTitle.MockTitle{
    title = "Original page",
    isRedirect = true,
    redirectTarget = {
        title = "Redirect target",
        isRedirect = true,
        redirectTarget = "Original page",
    },
}