Jump to content

Module:Mock title/testcases

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 14:56, 3 April 2021 (we need one test for cascading protection, as we only want to activate it when we have both the cascadingProtectionSources option plus one of the cascading protection level options). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local mMockTitle = require("Module:Mock title")
local ScribuntoUnit = require("Module:ScribuntoUnit")

local suite = ScribuntoUnit:new()

--------------------------------------------------------------------------------
-- MockTitle tests
--------------------------------------------------------------------------------

suite["test MockTitle: when no argument table is supplied, an error is raised"] = function (self)
	self:assertThrows(
		function () mMockTitle.MockTitle() end,
		"bad argument #1 to 'MockTitle' (table expected, got nil)"
	)
end

suite["test MockTitle: when no page argument is supplied, an error is raised"] = function (self)
	self:assertThrows(
		function () mMockTitle.MockTitle{} end,
		"bad named argument page to 'MockTitle' (string or number expected, got nil)"
	)
end

local pageNames = {
	"Example",
	"fr:Example",
	"Module:Sandbox",
	"Module:Sandbox/subpage",
	"mw:Test",
	"fr:b:Example",
}

for _, pageName in ipairs(pageNames) do
	suite[
		string.format(
			'test MockTitle: when using page name "%s", the prefixedText property equals the page name',
			pageName
		)
	] = function (self)
		self:assertEquals("Example", mMockTitle.MockTitle({page = "Example"}).prefixedText)
	end
end

local simplePropertyTestData = {
	{
		property = "id",
		page = "Example",
		value = 123456,
	},
	{
		property = "isRedirect",
		page = "Main Page",
		value = true,
	},
	{
		property = "exists",
		page = "Main Page",
		value = false,
	},
	{
		property = "contentModel",
		page = "Main Page",
		value = "foo",
	},
}

for _, testData in ipairs(simplePropertyTestData) do
	suite[string.format("test MockTitle: property %s is mocked using the %s option", testData.property, testData.property)] = function (self)
		local title = mMockTitle.MockTitle({page = testData.page, [testData.property] = testData.value})
		self:assertEquals(testData.value, title[testData.property])
	end
end

suite["test MockTitle: passing a page name to the redirectTitle option causes redirectTitle to be a MockTitle object with that page name"] = function (self)
	local title = mMockTitle.MockTitle({page = "Example", redirectTitle = "User:Example"})
	self:assertEquals("User:Example", title.redirectTitle.prefixedText)
end

suite["test MockTitle: passing an ID to the redirectTitle option causes redirectTitle to be a MockTitle object with that ID"] = function (self)
	local mainPageId = 15580374
	local title = mMockTitle.MockTitle({page = "Example", redirectTitle = mainPageId})
	self:assertEquals(mainPageId, title.redirectTitle.id)
end

suite["test MockTitle: passing an options table to the redirectTitle option causes redirectTitle to be a MockTitle object with those options"] = function (self)
	local title = mMockTitle.MockTitle({page = "Example", redirectTitle = {page = "User:Example/common.js", contentModel = "wikitext"}})
	self:assertEquals("User:Example/common.js", title.redirectTitle.prefixedText)
	self:assertEquals("wikitext", title.redirectTitle.contentModel)
end

suite["test MockTitle: passing a MockTitle object to the redirectTitle option causes redirectTitle to be that MockTitle object"] = function (self)
	local mockRedirectTitle = mMockTitle.MockTitle({page = "User:Example/common.js", contentModel = "wikitext"})
	local title = mMockTitle.MockTitle({page = "Example", redirectTitle = mockRedirectTitle})
	self:assertEquals("User:Example/common.js", title.redirectTitle.prefixedText)
	self:assertEquals("wikitext", title.redirectTitle.contentModel)
end

local protectionLevelTestData = {
	{
		optionName = "editProtection",
		optionValue = "sysop",
		expectedProtectionAction = "edit",
	},
	{
		optionName = "moveProtection",
		optionValue = "sysop",
		expectedProtectionAction = "move",
	},
	{
		optionName = "createProtection",
		optionValue = "sysop",
		expectedProtectionAction = "create",
	},
	{
		optionName = "uploadProtection",
		optionValue = "sysop",
		expectedProtectionAction = "upload",
	},
}

for _, testData in ipairs(protectionLevelTestData) do
	suite[
		string.format(
			'test MockTitle: when setting option %s to "%s", the protectionLevels table is updated accordingly',
			testData.optionName,
			testData.optionValue
		)
	] = function (self)
		local title = mMockTitle.MockTitle{page = "Example", [testData.optionName] = testData.optionValue}
		self:assertEquals(testData.optionValue, title.protectionLevels[testData.expectedProtectionAction][1])
	end
end

local cascadingProtectionLevelTestData = {
	{
		optionName = "cascadingEditProtection",
		optionValue = "sysop",
		expectedProtectionAction = "edit",
	},
	{
		optionName = "cascadingMoveProtection",
		optionValue = "sysop",
		expectedProtectionAction = "move",
	},
	{
		optionName = "cascadingCreateProtection",
		optionValue = "sysop",
		expectedProtectionAction = "create",
	},
	{
		optionName = "cascadingUploadProtection",
		optionValue = "sysop",
		expectedProtectionAction = "upload",
	},
}

for _, testData in ipairs(protectionLevelTestData) do
	suite[
		string.format(
			'test MockTitle: when setting option %s to "%s", '
			.. 'and when setting the cascadingProtectionSources option, '
			.. 'the cascadingProtection table is updated accordingly',
			testData.optionName,
			testData.optionValue
		)
	] = function (self)
		local title = mMockTitle.MockTitle{
			page = "Example",
			[testData.optionName] = testData.optionValue,
			cascadingProtectionSources = {"Example", "Example 2"},
		}
		self:assertEquals(
			testData.optionValue,
			title.cascadingProtection.restrictions[testData.expectedProtectionAction][1]
		)
	end
end

local fallbackPropertyTestData = {
	{
		property = "id",
		option = "id",
		page = "Example",
	},
	{
		property = "isRedirect",
		option = "isRedirect",
		page = "WP:ANI",
	},
	{
		property = "exists",
		option = "exists",
		page = "Non-existent title f292umz0tyi",
	},
	{
		property = "contentModel",
		option = "contentModel",
		page = "User:Example/common.js",
	},
	{
		property = "redirectTarget",
		option = "redirectTarget",
		page = "WP:ANI",
	},
	{
		property = "protectionLevels",
		option = "editProtection",
		page = "Main Page",
	},
	{
		property = "redirectTitle",
		option = "redirectTitle",
		page = "WP:ANI",
	},
	{
		property = "cascadingProtection",
		option = "cascadingEditProtection",
		page = "Main Page",
	},
	{
		property = "cascadingProtection",
		option = "cascadingProtectionSources",
		page = "Main Page",
	},
}

for _, testData in ipairs(fallbackPropertyTestData) do
	suite[string.format("test MockTitle: if no %s option is specified, the real %s value is used", testData.option, testData.property)] = function (self)
		suite:assertDeepEquals(
			mw.title.new(testData.page)[testData.property],
			mMockTitle.MockTitle{page = testData.page}[testData.property]
		)
	end
end

return suite