Jump to content

Module:Commons link/testcases

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Hike395 (talk | contribs) at 10:58, 29 September 2020 (rv). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = require('Module:UnitTests')

function p:testGallery()
	-- known link
	self:preprocess_equals('{{#invoke:Commons link| getGallery | Mount Whitney}}',
		'[[Commons:Mount Whitney|Mount Whitney]]')
	-- known link with linktext
	self:preprocess_equals('{{#invoke:Commons link| getGallery | Mount Whitney |linktext=Big Mountain}}',
		'[[Commons:Mount Whitney|Big Mountain]]')
	-- forced search
	self:preprocess_equals('{{#invoke:Commons link| getGallery | search=Mount Whitney}}',
		'[[Commons:Special:Search/Mount Whitney|Mount Whitney]]')
	-- forced search + linktext
	self:preprocess_equals('{{#invoke:Commons link| getGallery | search=Mount Whitney |linktext=Big Mountain}}',
		'[[Commons:Special:Search/Mount Whitney|Big Mountain]]')
	-- P935 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link| getGallery | qid=Q2217963}}',
		'[[Commons:Samuel Baker House|Samuel Baker House]]')
	-- P935 matches sitelink + linktext
	self:preprocess_equals('{{#invoke:Commons link| getGallery | qid=Q2217963 |linktext=Big House}}',
		'[[Commons:Samuel Baker House|Big House]]')
	-- sitelink only
	self:preprocess_equals('{{#invoke:Commons link| getGallery | qid=Q816044 | lcfirst=1}}',
		'[[Commons:Bellows Falls, Vermont|bellows Falls, Vermont]]')
	-- sitelink + linktext
	self:preprocess_equals('{{#invoke:Commons link| getGallery | qid=Q816044|linktext=Little Town}}',
		'[[Commons:Bellows Falls, Vermont|Little Town]]')
	-- sitelink and P935 inconsistent
	self:preprocess_equals('{{#invoke:Commons link| getGallery | qid=Q139289}}',
		'[[Commons:Special:Search/Boeing 737 MAX|Boeing 737 MAX]]')
	-- sitelink and P935 inconsistent + linktext
	self:preprocess_equals('{{#invoke:Commons link| getGallery | qid=Q139289|linktext=Not a plane}}',
		'[[Commons:Special:Search/Boeing 737 MAX|Not a plane]]')
	-- no sitelink or P935
	self:preprocess_equals('{{#invoke:Commons link| getGallery | qid=Q29017978}}',
		'[[Commons:Special:Search/Bell FCX-001|Bell FCX-001]]')
	-- no sitelink or P935, but linktext
	self:preprocess_equals('{{#invoke:Commons link| getGallery | qid=Q29017978|linktext=Medium Helicopter}}',
		'[[Commons:Special:Search/Bell FCX-001|Medium Helicopter]]')
end

function p:testCategory()
	-- known link
	self:preprocess_equals('{{#invoke:Commons link| getCategory | Mount Whitney}}',
		'[[Commons:Category:Mount Whitney|Mount Whitney]]')
	-- known link with linktext
	self:preprocess_equals('{{#invoke:Commons link| getCategory | Mount Whitney |linktext=Big Mountain}}',
		'[[Commons:Category:Mount Whitney|Big Mountain]]')
	-- forced search
	self:preprocess_equals('{{#invoke:Commons link| getCategory | search=Mount Whitney}}',
		'[[Commons:Special:Search/Category:Mount Whitney|Mount Whitney]]')
	-- forced search + linktext
	self:preprocess_equals('{{#invoke:Commons link| getCategory | search=Mount Whitney |linktext=Big Mountain}}',
		'[[Commons:Special:Search/Category:Mount Whitney|Big Mountain]]')
	-- P373 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link| getCategory | qid=Q2601154|lcfirst=1}}',
		'[[Commons:Category:Agama lionotus|agama lionotus]]')
	-- P373 matches sitelink + linktext
	self:preprocess_equals('{{#invoke:Commons link| getCategory | qid=Q2601154|linktext=Small Lizard|lcfirst=1}}',
		'[[Commons:Category:Agama lionotus|small Lizard]]')
	-- P910 matches P373
	self:preprocess_equals('{{#invoke:Commons link| getCategory | qid=Q853037}}',
		'[[Commons:Category:Bánhida|Bánhida]]')
	-- P910 matches P373 + linktext
	self:preprocess_equals('{{#invoke:Commons link| getCategory | qid=Q853037|linktext=Old Neighborhood}}',
		'[[Commons:Category:Bánhida|Old Neighborhood]]')
	-- P1754 matches P373
	self:preprocess_equals('{{#invoke:Commons link| getCategory | qid=Q2551907}}',
		'[[Commons:Category:Lakes of Turkey|Lakes of Turkey]]')
	-- P1754 matches P373 + linktext
	self:preprocess_equals('{{#invoke:Commons link| getCategory | qid=Q2551907|linktext=Turkey Lakes}}',
		'[[Commons:Category:Lakes of Turkey|Turkey Lakes]]')
	-- inconsistency between P373 and P1754
	self:preprocess_equals('{{#invoke:Commons link| getCategory | qid=Q1002229}}',
		'[[Commons:Special:Search/Category:Municipality of Vipava|Municipality of Vipava]]')
	-- inconsistency between P373 and P1754 + linktext
	self:preprocess_equals('{{#invoke:Commons link| getCategory | qid=Q1002229|linktext=Little Town}}',
		'[[Commons:Special:Search/Category:Municipality of Vipava|Little Town]]')
	-- missing data
	self:preprocess_equals('{{#invoke:Commons link| getCategory | qid=Q2963317}}',
		'[[Commons:Special:Search/Category:Chicago Lawn, Chicago|Chicago Lawn, Chicago]]')
	-- missing data + linktext
	self:preprocess_equals('{{#invoke:Commons link| getCategory | qid=Q2963317|linktext=Big Neighborhood}}',
		'[[Commons:Special:Search/Category:Chicago Lawn, Chicago|Big Neighborhood]]')
	
end

function p:testGalleryOrCategory()
	-- known link
	self:preprocess_equals('{{#invoke:Commons link| getGalleryOrCategory | Mount Whitney}}',
		'[[Commons:Mount Whitney|Mount Whitney]]')
	-- known link with linktext
	self:preprocess_equals('{{#invoke:Commons link| getGalleryOrCategory | Mount Whitney |linktext=Big Mountain}}',
		'[[Commons:Mount Whitney|Big Mountain]]')
	-- forced search
	self:preprocess_equals('{{#invoke:Commons link| getGalleryOrCategory | search=Mount Whitney}}',
		'[[Commons:Special:Search/Mount Whitney|Mount Whitney]]')
	-- forced search + linktext
	self:preprocess_equals('{{#invoke:Commons link| getGalleryOrCategory | search=Mount Whitney |linktext=Big Mountain}}',
		'[[Commons:Special:Search/Mount Whitney|Big Mountain]]')
	-- P935 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link| getGalleryOrCategory | qid=Q2217963}}',
		'[[Commons:Samuel Baker House|Samuel Baker House]]')
	-- sitelink only
	self:preprocess_equals('{{#invoke:Commons link| getGalleryOrCategory | qid=Q816044}}',
		'[[Commons:Bellows Falls, Vermont|Bellows Falls, Vermont]]')
	-- sitelink and P935 inconsistent, fallback to Category instead
	self:preprocess_equals('{{#invoke:Commons link| getGalleryOrCategory | qid=Q139289}}',
		'[[Commons:Category:Boeing 737 MAX|Boeing 737 MAX]]')
	-- P373 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link| getGalleryOrCategory | qid=Q2601154}}',
		'[[Commons:Category:Agama lionotus|Agama lionotus]]')
	-- P910 matches P373
	self:preprocess_equals('{{#invoke:Commons link| getGalleryOrCategory | qid=Q853037}}',
		'[[Commons:Category:Bánhida|Bánhida]]')
	-- P1754 matches P373
	self:preprocess_equals('{{#invoke:Commons link| getGalleryOrCategory | qid=Q2551907}}',
		'[[Commons:Category:Lakes of Turkey|Lakes of Turkey]]')
	-- missing gallery, inconsistent category
	self:preprocess_equals('{{#invoke:Commons link| getGalleryOrCategory | qid=Q1002229}}',
		'[[Commons:Special:Search/Municipality of Vipava|Municipality of Vipava]]')
	-- missing data
	self:preprocess_equals('{{#invoke:Commons link| getGalleryOrCategory | qid=Q2963317}}',
		'[[Commons:Special:Search/Chicago Lawn, Chicago|Chicago Lawn, Chicago]]')
end


function p:testHasGallery()
	-- P935 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link| hasGallery | qid=Q2217963}}','Samuel Baker House')
	-- sitelink only
	self:preprocess_equals('{{#invoke:Commons link| hasGallery | qid=Q816044}}','Bellows Falls, Vermont')
	-- sitelink and P935 inconsistent
	self:preprocess_equals('{{#invoke:Commons link| hasGallery | qid=Q139289}}','')
	-- no sitelink or P935 or category
	self:preprocess_equals('{{#invoke:Commons link| hasGallery | qid=Q29017978}}','')
	-- P373 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link| hasGallery | qid=Q2601154}}','')
	-- P910 matches P373
	self:preprocess_equals('{{#invoke:Commons link| hasGallery | qid=Q853037}}','')
	-- P1754 matches P373
	self:preprocess_equals('{{#invoke:Commons link| hasGallery | qid=Q2551907}}','')
	-- missing gallery, inconsistent category
	self:preprocess_equals('{{#invoke:Commons link| hasGallery | qid=Q1002229}}','')
	-- missing data
	self:preprocess_equals('{{#invoke:Commons link| hasGallery | qid=Q2963317}}','')
end


function p:testHasCategory()
	-- P935 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link| hasCategory | qid=Q2217963}}','')
	-- sitelink only
	self:preprocess_equals('{{#invoke:Commons link| hasCategory | qid=Q816044}}','Category:Bellows Falls, Vermont')
	-- sitelink and P935 inconsistent, fallback to Category instead
	self:preprocess_equals('{{#invoke:Commons link| hasCategory | qid=Q139289}}','Category:Boeing 737 MAX')
	-- P373 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link| hasCategory | qid=Q2601154}}','Category:Agama lionotus')
	-- P910 matches P373
	self:preprocess_equals('{{#invoke:Commons link| hasCategory | qid=Q853037}}','Category:Bánhida')
	-- P1754 matches P373
	self:preprocess_equals('{{#invoke:Commons link| hasCategory | qid=Q2551907}}','Category:Lakes of Turkey')
	-- missing gallery, inconsistent category
	self:preprocess_equals('{{#invoke:Commons link| hasCategory | qid=Q1002229}}','')
	-- missing data
	self:preprocess_equals('{{#invoke:Commons link| hasCategory | qid=Q2963317}}','')
end

function p:testHasGalleryOrCategory()
	-- P935 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link| hasGalleryOrCategory | qid=Q2217963}}','Samuel Baker House')
	-- sitelink only
	self:preprocess_equals('{{#invoke:Commons link| hasGalleryOrCategory | qid=Q816044}}','Bellows Falls, Vermont')
	-- sitelink and P935 inconsistent, fallback to Category instead
	self:preprocess_equals('{{#invoke:Commons link| hasGalleryOrCategory | qid=Q139289}}','Category:Boeing 737 MAX')
	-- P373 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link| hasGalleryOrCategory | qid=Q2601154}}','Category:Agama lionotus')
	-- P910 matches P373
	self:preprocess_equals('{{#invoke:Commons link| hasGalleryOrCategory | qid=Q853037}}','Category:Bánhida')
	-- P1754 matches P373
	self:preprocess_equals('{{#invoke:Commons link| hasGalleryOrCategory | qid=Q2551907}}','Category:Lakes of Turkey')
	-- missing gallery, inconsistent category
	self:preprocess_equals('{{#invoke:Commons link| hasGalleryOrCategory | qid=Q1002229}}','')
	-- missing data
	self:preprocess_equals('{{#invoke:Commons link| hasGalleryOrCategory | qid=Q2963317}}','')
end


function p:testGalleryAndCategory()
	-- known link
	self:preprocess_equals('{{#invoke:Commons link| getGalleryAndCategory | Mount Whitney}}',
		'[[Commons:Mount Whitney|Mount Whitney]]')
	-- known link with linktext
	self:preprocess_equals('{{#invoke:Commons link| getGalleryAndCategory | Mount Whitney |linktext=Big Mountain | italic=1}}',
		'<i>[[Commons:Mount Whitney|Big Mountain]]</i>')
	-- known category
	self:preprocess_equals('{{#invoke:Commons link| getGalleryAndCategory | | Mount Whitney | bold=1}}',
		'<b>[[Commons:Category:Mount Whitney|Mount Whitney]]</b>')
	-- known category with categorytext
	self:preprocess_equals('{{#invoke:Commons link| getGalleryAndCategory | | Mount Whitney |linktext=Big Mountain|bold=1|italic=1}}',
		'<b><i>[[Commons:Category:Mount Whitney|Big Mountain]]</i></b>')
	-- known link and category
	self:preprocess_equals('{{#invoke:Commons link| getGalleryAndCategory | Mount Whitney | Mount Whitney}}',
		'[[Commons:Mount Whitney|Mount Whitney]] ([[Commons:Category:Mount Whitney|category]])')
	-- known link and category with linktext
	self:preprocess_equals('{{#invoke:Commons link| getGalleryAndCategory | Mount Whitney | Mount Whitney | linktext=Big Mountain|bold=1|italic=1}}',
		'<b><i>[[Commons:Mount Whitney|Big Mountain]]</i></b> ([[Commons:Category:Mount Whitney|category]])')
	-- only gallery exists
	self:preprocess_equals('{{#invoke:Commons link| getGalleryAndCategory | qid=Q2217963|italic=1}}',
		'<i>[[Commons:Samuel Baker House|Samuel Baker House]]</i>')
	-- both gallery and category exist
	self:preprocess_equals('{{#invoke:Commons link| getGalleryAndCategory | qid=Q816044|bold=1|categoryText=cat}}',
		'<b>[[Commons:Bellows Falls, Vermont|Bellows Falls, Vermont]]</b> ([[Commons:Category:Bellows Falls, Vermont|cat]])')
	-- sitelink and P935 inconsistent, fallback to Category instead
	self:preprocess_equals('{{#invoke:Commons link| getGalleryAndCategory | qid=Q139289}}',
		'[[Commons:Category:Boeing 737 MAX|Boeing 737 MAX]]')
	-- P373 matches sitelink, category only
	self:preprocess_equals('{{#invoke:Commons link| getGalleryAndCategory | qid=Q2601154|bold=1}}',
		'<b>[[Commons:Category:Agama lionotus|Agama lionotus]]</b>')
	-- P910 matches P373, category only
	self:preprocess_equals('{{#invoke:Commons link| getGalleryAndCategory | qid=Q853037|italic=1|bold=1}}',
		'<b><i>[[Commons:Category:Bánhida|Bánhida]]</i></b>')
	-- P1754 matches P373, category only
	self:preprocess_equals('{{#invoke:Commons link| getGalleryAndCategory | qid=Q2551907}}',
		'[[Commons:Category:Lakes of Turkey|Lakes of Turkey]]')
	-- missing gallery, inconsistent category
	self:preprocess_equals('{{#invoke:Commons link| getGalleryAndCategory | qid=Q1002229|italic=1|linktext=Vipava|categoryText=cat}}',
		'<i>[[Commons:Special:Search/Municipality of Vipava|Vipava]]</i> ([[Commons:Special:Search/Category:Municipality of Vipava|cat]])')
	-- missing data
	self:preprocess_equals('{{#invoke:Commons link| getGalleryAndCategory | qid=Q2963317|bold=1|linktext=Chicago Lawn|oneSearch=1}}',
		'<b>[[Commons:Special:Search/Chicago Lawn, Chicago|Chicago Lawn]]</b>')
end


function p:testTitleQID()
	self:preprocess_equals('{{#invoke:Commons link| getTitleQID }}','Commons link/testcases,Module talk,nil')
	self:preprocess_equals('{{#invoke:Commons link| getTitleQID | Q42 | 1}}','Douglas Adams,,Q42')
	self:preprocess_equals('{{#invoke:Commons link| getTitleQID | Q15048757 | 1}}','Python (genus),Category,Q15048757')
	-- no page corresponding to QID
	self:preprocess_equals('{{#invoke:Commons link| getTitleQID | Q0}}',',Module talk,Q0')
	-- QID doesn't link back to en
	self:preprocess_equals('{{#invoke:Commons link| getTitleQID | Q68979196 | 1}}',',,Q68979196')
	-- gibberish QID
	self:preprocess_equals('{{#invoke:Commons link| getTitleQID | 1234}}','Commons link/testcases,Module talk,nil')
end

function p:testLookupFallback()
	-- P910 doesn't exist
	self:preprocess_equals('{{#invoke:Commons link| lookupFallback | Q2217963 | P910}}','nil')
	-- normal P910
	self:preprocess_equals('{{#invoke:Commons link| lookupFallback | Q853037 | P910}}','Bánhida')
	-- normal P1754
	self:preprocess_equals('{{#invoke:Commons link| lookupFallback | Q2551907 | P1754}}','Lakes of Turkey')
	-- gibberish prop string
	self:preprocess_equals('{{#invoke:Commons link| lookupFallback | Q2551907 | xyzzy}}','nil')
end

function p:testTracking()
	local galPrefix = '[[Category:Commons link '
	local catPrefix = '[[Category:Commons category link '
	local onWikidata = 'is on Wikidata]]'
	local definedAsPagename = 'is defined as the pagename]]'
	local locallyDefined = 'is locally defined]]'
	local fromWikidata = 'from Wikidata]]'
	local isPagename = 'is the pagename]]'
	-- default gallery is specified and is Commons gallery on wikidata
	self:preprocess_equals('{{#invoke:Commons link| tracking | Samuel Baker House | fetchGallery = 1 | fetchCategory = 1 | qid=Q2217963}}',
		galPrefix..onWikidata,{nowiki=1})
	-- default gallery is specified and is Commons category on wikidata
	self:preprocess_equals('{{#invoke:Commons link| tracking | Fado | fetchGallery=1 | fetchCategory=1 | qid=Q185676}}',
		galPrefix..onWikidata,{nowiki=1})
	-- default category is specified and is Commons category on wikidata
	self:preprocess_equals('{{#invoke:Commons link| tracking | Category:Fado | fetchCategory=1 | qid=Q8442543}}',
		catPrefix..onWikidata,{nowiki=1})
	-- inconsistent wikidata returns nil, so doesn't match title. Called from Template:Commons
	self:preprocess_equals('{{#invoke:Commons link| tracking | Municipality of Vipava | fetchGallery=1 | fetchCategory=1 | qid=Q1002229}}',
		galPrefix..definedAsPagename,{nowiki=1})
	-- inconsistent wikidata returns nil, so doesn't match title. Called from Template:Commonscat
	self:preprocess_equals('{{#invoke:Commons link| tracking | Municipality of Vipava | fetchCategory=1 | qid=Q1002229}}',
		catPrefix..definedAsPagename,{nowiki=1})
	-- Default string is not the same as either title or wikidata. Called from Template:Commons
	self:preprocess_equals('{{#invoke:Commons link| tracking | Bellows Falls | fetchGallery = 1 | fetchCategory=1 | qid=Q816044}}',
		galPrefix..locallyDefined,{nowiki=1})
	-- Default string is not the same as either title or wikidata. Called from Template:Commonscat
	self:preprocess_equals('{{#invoke:Commons link| tracking | Bellows Falls | fetchCategory=1 | qid=Q816044}}',
		catPrefix..locallyDefined,{nowiki=1})
	-- No string. Commons gallery on Wikidata
	self:preprocess_equals('{{#invoke:Commons link| tracking || fetchGallery=1 | fetchCategory=1 | qid=Q235539}}',
		galPrefix..fromWikidata,{nowiki=1})
	-- No string. Commons category on Wikidata
	self:preprocess_equals('{{#invoke:Commons link| tracking || fetchCategory=1 | qid=Q121319}}',
		catPrefix..fromWikidata,{nowiki=1})
	-- No string. Nothing on Wikidata. Called from Template:Commons
	self:preprocess_equals('{{#invoke:Commons link| tracking | fetchGallery=1| fetchCategory=1 | qid=Q5598232}}',
		galPrefix..isPagename,{nowiki=1})
	-- No string. Nothing on Wikidata. Called from Template:CommonsCat
	self:preprocess_equals('{{#invoke:Commons link| tracking || fetchCategory=1 | qid=Q5598232}}',
		catPrefix..isPagename,{nowiki=1})
end

return p