Documentation for this module may be created at Module:Commons link/sandbox/testcases/doc

local p = require('Module:UnitTests')

function p:testGallery()
	-- known link
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | Mount Whitney}}',
		'[[Commons:Mount Whitney|Mount Whitney]]')
	-- known link with linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | Mount Whitney |linktext=Big Mountain}}',
		'[[Commons:Mount Whitney|Big Mountain]]')
	-- forced search
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | search=Mount Whitney}}',
		'[[Commons:Special:Search/Mount Whitney|Mount Whitney]]')
	-- forced search + linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | search=Mount Whitney |linktext=Big Mountain}}',
		'[[Commons:Special:Search/Mount Whitney|Big Mountain]]')
	-- P935 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q2217963}}',
		'[[Commons:Samuel Baker House|Samuel Baker House]]')
	-- P935 matches sitelink + linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q2217963 |linktext=Big House}}',
		'[[Commons:Samuel Baker House|Big House]]')
	-- sitelink only, lcfirst
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q816044 | lcfirst=1}}',
		'[[Commons:Bellows Falls, Vermont|bellows Falls, Vermont]]')
	-- sitelink only, bold
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q816044 | bold=1}}',
		'[[Commons:Bellows Falls, Vermont|<span style="font-weight:bold;">Bellows Falls, Vermont</span>]]')
	-- sitelink only, italic
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q816044 | italic=1}}',
		'[[Commons:Bellows Falls, Vermont|<span style="font-style:italic;">Bellows Falls, Vermont</span>]]')
	-- sitelink only, nowrap
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q816044 | nowrap=1}}',
		'[[Commons:Bellows Falls, Vermont|<span style="white-space:nowrap;">Bellows Falls, Vermont</span>]]')
	-- sitelink + linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q816044|linktext=Little Town}}',
		'[[Commons:Bellows Falls, Vermont|Little Town]]')
	-- sitelink and P935 inconsistent
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q139289}}',
		'[[Commons:Special:Search/Boeing 737 MAX|Boeing 737 MAX]]')
	-- sitelink and P935 inconsistent + linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| 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/sandbox| getGallery | qid=Q29017978}}',
		'[[Commons:Special:Search/Bell FCX-001|Bell FCX-001]]')
	-- no sitelink or P935, but linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| 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/sandbox| getCategory | Mount Whitney}}',
		'[[Commons:Category:Mount Whitney|Mount Whitney]]')
	-- known link with linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | Mount Whitney |linktext=Big Mountain}}',
		'[[Commons:Category:Mount Whitney|Big Mountain]]')
	-- forced search
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | search=Mount Whitney}}',
		'[[Commons:Special:Search/Category:Mount Whitney|Mount Whitney]]')
	-- forced search + linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | search=Mount Whitney |linktext=Big Mountain}}',
		'[[Commons:Special:Search/Category:Mount Whitney|Big Mountain]]')
	-- P373 matches sitelink + lcfirst
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2601154|lcfirst=1}}',
		'[[Commons:Category:Agama lionotus|agama lionotus]]')
	-- bold
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2601154|bold=1}}',
		'[[Commons:Category:Agama lionotus|<span style="font-weight:bold;">Agama lionotus</span>]]')
	-- italic
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2601154|italic=1}}',
		'[[Commons:Category:Agama lionotus|<span style="font-style:italic;">Agama lionotus</span>]]')
	-- nowrap
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2601154|nowrap=1}}',
		'[[Commons:Category:Agama lionotus|<span style="white-space:nowrap;">Agama lionotus</span>]]')
	-- P373 matches sitelink + linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2601154|linktext=Small Lizard|lcfirst=1}}',
		'[[Commons:Category:Agama lionotus|small Lizard]]')
	-- P910 matches P373
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q853037}}',
		'[[Commons:Category:Bánhida|Bánhida]]')
	-- P910 matches P373 + linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q853037|linktext=Old Neighborhood}}',
		'[[Commons:Category:Bánhida|Old Neighborhood]]')
	-- P1754 matches P373
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2551907}}',
		'[[Commons:Category:Lakes of Turkey|Lakes of Turkey]]')
	-- P1754 matches P373 + linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2551907|linktext=Turkey Lakes}}',
		'[[Commons:Category:Lakes of Turkey|Turkey Lakes]]')
	-- inconsistency between P373 and P1754
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q1002229}}',
		'[[Commons:Category:Municipality of Vipava|Municipality of Vipava]]')
	-- test fallback
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q21032720 |fallback=Boing}}',
		'[[Commons:Special:Search/Category:Boing|Boing]]')
	-- inconsistency between P373 and P1754 + linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q1002229|linktext=Little Town}}',
		'[[Commons:Category:Municipality of Vipava|Little Town]]')
	-- missing data
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2963317}}',
		'[[Commons:Special:Search/Category:Chicago Lawn, Chicago|Chicago Lawn, Chicago]]')
	-- missing data + linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| 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/sandbox| getGalleryOrCategory | Mount Whitney}}',
		'[[Commons:Mount Whitney|Mount Whitney]]')
	-- known link with linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | Mount Whitney |linktext=Big Mountain}}',
		'[[Commons:Mount Whitney|Big Mountain]]')
	-- forced search
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | search=Mount Whitney}}',
		'[[Commons:Special:Search/Mount Whitney|Mount Whitney]]')
	-- forced search + linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | search=Mount Whitney |linktext=Big Mountain}}',
		'[[Commons:Special:Search/Mount Whitney|Big Mountain]]')
	-- P935 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | qid=Q2217963}}',
		'[[Commons:Samuel Baker House|Samuel Baker House]]')
	-- sitelink only
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | qid=Q816044}}',
		'[[Commons:Bellows Falls, Vermont|Bellows Falls, Vermont]]')
	-- inconsistent data, fall back to search
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | qid=Q21032720}}',
		'[[Commons:Special:Search/Russian military intervention in the Syrian civil war|Russian military intervention in the Syrian civil war]]')
	-- P373 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | qid=Q2601154}}',
		'[[Commons:Category:Agama lionotus|Agama lionotus]]')
	-- P910 matches P373
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | qid=Q853037}}',
		'[[Commons:Category:Bánhida|Bánhida]]')
	-- P1754 matches P373
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | qid=Q2551907}}',
		'[[Commons:Category:Lakes of Turkey|Lakes of Turkey]]')
	-- missing gallery, inconsistent category
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | qid=Q1002229}}',
		'[[Commons:Category:Municipality of Vipava|Municipality of Vipava]]')
	-- test fallback
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | fallback=Boing | qid=Q21032720}}',
		'[[Commons:Special:Search/Boing|Boing]]')
	-- missing data
	self:preprocess_equals('{{#invoke:Commons link/sandbox| 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/sandbox| hasGallery | qid=Q2217963}}','Samuel Baker House')
	-- sitelink only
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGallery | qid=Q816044}}','Bellows Falls, Vermont')
	-- sitelink and P935 inconsistent
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGallery | qid=Q139289}}','')
	-- no sitelink or P935 or category
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGallery | qid=Q29017978}}','')
	-- P373 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGallery | qid=Q2601154}}','')
	-- P910 matches P373
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGallery | qid=Q853037}}','')
	-- P1754 matches P373
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGallery | qid=Q2551907}}','')
	-- missing gallery, inconsistent category
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGallery | qid=Q1002229}}','')
	-- missing data
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGallery | qid=Q2963317}}','')
end


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

function p:testHasGalleryOrCategory()
	-- P935 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGalleryOrCategory | qid=Q2217963}}','Samuel Baker House')
	-- sitelink only
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGalleryOrCategory | qid=Q816044}}','Bellows Falls, Vermont')
	-- sitelink and P935 inconsistent
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGalleryOrCategory | qid=Q139289}}','Category:Boeing 737 MAX')
	-- P373 matches sitelink
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGalleryOrCategory | qid=Q2601154}}','Category:Agama lionotus')
	-- P910 matches P373
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGalleryOrCategory | qid=Q853037}}','Category:Bánhida')
	-- P1754 matches P373
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGalleryOrCategory | qid=Q2551907}}','Category:Lakes of Turkey')
	-- missing gallery
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGalleryOrCategory | qid=Q1002229}}','Category:Municipality of Vipava')
	-- missing data
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGalleryOrCategory | qid=Q2963317}}','')
	-- another test
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGalleryOrCategory | qid=Q185676}}','Category:Fado')
	-- inconsistent results, P373 should not be invoked here
	self:preprocess_equals('{{#invoke:Commons link/sandbox| hasGalleryOrCategory | qid=Q477248}}','')
end


function p:testGalleryAndCategory()
	-- known link
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | Mount Whitney}}',
		'[[Commons:Mount Whitney|Mount Whitney]]')
	-- known link with linktext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | Mount Whitney |linktext=Big Mountain | italic=1}}',
		'[[Commons:Mount Whitney|<span style="font-style:italic;">Big Mountain</span>]]')
	-- known category
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | | Mount Whitney | bold=1}}',
		'[[Commons:Category:Mount Whitney|<span style="font-weight:bold;">Mount Whitney</span>]]')
	-- known category with categorytext
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | | Mount Whitney |linktext=Big Mountain|bold=1|italic=1}}',
		'[[Commons:Category:Mount Whitney|<span style="font-style:italic; font-weight:bold;">Big Mountain</span>]]')
	-- known link and category
	self:preprocess_equals('{{#invoke:Commons link/sandbox| 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/sandbox| getGalleryAndCategory | Mount Whitney | Mount Whitney | linktext=Big Mountain|bold=1|italic=1}}',
		'[[Commons:Mount Whitney|<span style="font-style:italic; font-weight:bold;">Big Mountain</span>]] ([[Commons:Category:Mount Whitney|category]])')
	-- only gallery exists
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | qid=Q2217963|italic=1}}',
		'[[Commons:Samuel Baker House|<span style="font-style:italic;">Samuel Baker House</span>]]')
	-- both gallery and category exist
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | qid=Q816044|bold=1|categoryText=cat}}',
		'[[Commons:Bellows Falls, Vermont|<span style="font-weight:bold;">Bellows Falls, Vermont</span>]] ([[Commons:Category:Bellows Falls, Vermont|cat]])')
	-- P373 matches sitelink, category only
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | qid=Q2601154|bold=1}}',
		'[[Commons:Category:Agama lionotus|<span style="font-weight:bold;">Agama lionotus</span>]]')
	-- P910 matches P373, category only
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | qid=Q853037|italic=1|bold=1}}',
		'[[Commons:Category:Bánhida|<span style="font-style:italic; font-weight:bold;">Bánhida</span>]]')
	-- P1754 matches P373, category only
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | qid=Q2551907}}',
		'[[Commons:Category:Lakes of Turkey|Lakes of Turkey]]')
	-- missing data
	self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | qid=Q2963317|bold=1|linktext=Chicago Lawn|oneSearch=1}}',
		'[[Commons:Special:Search/Chicago Lawn, Chicago|<span style="font-weight:bold;">Chicago Lawn</span>]]')
end


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

function p:testLookupFallback()
	-- P910 doesn't exist
	self:preprocess_equals('{{#invoke:Commons link/sandbox| lookupFallback | Q2217963 | P910}}','nil')
	-- normal P910
	self:preprocess_equals('{{#invoke:Commons link/sandbox| lookupFallback | Q853037 | P910}}','Bánhida')
	-- normal P1754
	self:preprocess_equals('{{#invoke:Commons link/sandbox| lookupFallback | Q2551907 | P1754}}','Lakes of Turkey')
	-- gibberish prop string
	self:preprocess_equals('{{#invoke:Commons link/sandbox| 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]]'
	local galInconsistent = '[[Category:Inconsistent wikidata for Commons gallery]]'
	local catInconsistent = '[[Category:Inconsistent wikidata for Commons category]]'
	-- default gallery is specified and is Commons gallery on wikidata
	self:preprocess_equals('{{#invoke:Commons link/sandbox| 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/sandbox| tracking | Fado | fetchGallery=1 | fetchCategory=1 | qid=Q185676}}',
		galPrefix..definedAsPagename,{nowiki=1})
	-- default category is specified and is Commons category on wikidata
	self:preprocess_equals('{{#invoke:Commons link/sandbox| 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/sandbox| tracking | Municipality of Vipava | fetchGallery=1 | fetchCategory=1 | qid=Q1002229}}',
		galPrefix..definedAsPagename,{nowiki=1})
	-- Default string is not the same as either title or wikidata. Called from Template:Commons
	self:preprocess_equals('{{#invoke:Commons link/sandbox| 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/sandbox| tracking | Category:Bellows Falls | fetchCategory=1 | qid=Q816044}}',
		catPrefix..locallyDefined,{nowiki=1})
	-- No string. Commons gallery on Wikidata
	self:preprocess_equals('{{#invoke:Commons link/sandbox| tracking || fetchGallery=1 | fetchCategory=1 | qid=Q235539}}',
		galPrefix..fromWikidata,{nowiki=1})
	-- No string. Commons category on Wikidata
	self:preprocess_equals('{{#invoke:Commons link/sandbox| tracking || fetchCategory=1 | qid=Q121319}}',
		catPrefix..fromWikidata,{nowiki=1})
	-- No string. Nothing on Wikidata. Called from Template:Commons
	self:preprocess_equals('{{#invoke:Commons link/sandbox| tracking | fetchGallery=1| fetchCategory=1 | qid=Q2963317}}',
		galPrefix..isPagename,{nowiki=1})
	-- No string. Nothing on Wikidata. Called from Template:CommonsCat
	self:preprocess_equals('{{#invoke:Commons link/sandbox| tracking || fetchCategory=1 | qid=Q2963317}}',
		catPrefix..isPagename,{nowiki=1})
	-- Inconsistent wikidata for category
	self:preprocess_equals('{{#invoke:Commons link/sandbox| tracking |fetchCategory=1|qid=Q21032720}}',
		catInconsistent,{nowiki=1})
end

return p