Module:Commons link/sandbox/testcases

local p = require('Module:UnitTests')

function p:testGallery()

-- known link

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | Mount Whitney}}',

'Mount Whitney')

-- known link with linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | Mount Whitney |linktext=Big Mountain}}',

'Big Mountain')

-- forced search

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | search=Mount Whitney}}',

'Mount Whitney')

-- forced search + linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | search=Mount Whitney |linktext=Big Mountain}}',

'Big Mountain')

-- P935 matches sitelink

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q2217963}}',

'Samuel Baker House')

-- P935 matches sitelink + linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q2217963 |linktext=Big House}}',

'Big House')

-- sitelink only, lcfirst

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q816044 | lcfirst=1}}',

'bellows Falls, Vermont')

-- sitelink only, bold

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q816044 | bold=1}}',

'Bellows Falls, Vermont')

-- sitelink only, italic

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q816044 | italic=1}}',

'Bellows Falls, Vermont')

-- sitelink only, nowrap

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q816044 | nowrap=1}}',

'Bellows Falls, Vermont')

-- sitelink + linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q816044|linktext=Little Town}}',

'Little Town')

-- sitelink and P935 inconsistent

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q139289}}',

'Boeing 737 MAX')

-- sitelink and P935 inconsistent + linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q139289|linktext=Not a plane}}',

'Not a plane')

-- no sitelink or P935

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q29017978}}',

'Bell FCX-001')

-- no sitelink or P935, but linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGallery | qid=Q29017978|linktext=Medium Helicopter}}',

'Medium Helicopter')

end

function p:testCategory()

-- known link

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | Mount Whitney}}',

'Mount Whitney')

-- known link with linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | Mount Whitney |linktext=Big Mountain}}',

'Big Mountain')

-- forced search

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | search=Mount Whitney}}',

'Mount Whitney')

-- forced search + linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | search=Mount Whitney |linktext=Big Mountain}}',

'Big Mountain')

-- P373 matches sitelink + lcfirst

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2601154|lcfirst=1}}',

'agama lionotus')

-- bold

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2601154|bold=1}}',

'Agama lionotus')

-- italic

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2601154|italic=1}}',

'Agama lionotus')

-- nowrap

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2601154|nowrap=1}}',

'Agama lionotus')

-- P373 matches sitelink + linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2601154|linktext=Small Lizard|lcfirst=1}}',

'small Lizard')

-- P910 matches P373

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q853037}}',

'Bánhida')

-- P910 matches P373 + linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q853037|linktext=Old Neighborhood}}',

'Old Neighborhood')

-- P1754 matches P373

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2551907}}',

'Lakes of Turkey')

-- P1754 matches P373 + linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2551907|linktext=Turkey Lakes}}',

'Turkey Lakes')

-- inconsistency between P373 and P1754

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q1002229}}',

'Municipality of Vipava')

-- test fallback

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q21032720 |fallback=Boing}}',

'Boing')

-- inconsistency between P373 and P1754 + linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q1002229|linktext=Little Town}}',

'Little Town')

-- missing data

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2963317}}',

'Chicago Lawn, Chicago')

-- missing data + linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getCategory | qid=Q2963317|linktext=Big Neighborhood}}',

'Big Neighborhood')

end

function p:testGalleryOrCategory()

-- known link

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | Mount Whitney}}',

'Mount Whitney')

-- known link with linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | Mount Whitney |linktext=Big Mountain}}',

'Big Mountain')

-- forced search

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | search=Mount Whitney}}',

'Mount Whitney')

-- forced search + linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | search=Mount Whitney |linktext=Big Mountain}}',

'Big Mountain')

-- P935 matches sitelink

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | qid=Q2217963}}',

'Samuel Baker House')

-- sitelink only

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | qid=Q816044}}',

'Bellows Falls, Vermont')

-- inconsistent data, fall back to search

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | qid=Q21032720}}',

'Russian intervention in the Syrian civil war')

-- P373 matches sitelink

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | qid=Q2601154}}',

'Agama lionotus')

-- P910 matches P373

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | qid=Q853037}}',

'Bánhida')

-- P1754 matches P373

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | qid=Q2551907}}',

'Lakes of Turkey')

-- missing gallery, inconsistent category

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | qid=Q1002229}}',

'Municipality of Vipava')

-- test fallback

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | fallback=Boing | qid=Q21032720}}',

'Boing')

-- missing data

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryOrCategory | qid=Q2963317}}',

'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()

-- 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')

end

function p:testGalleryAndCategory()

-- known link

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | Mount Whitney}}',

'Mount Whitney')

-- known link with linktext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | Mount Whitney |linktext=Big Mountain | italic=1}}',

'Big Mountain')

-- known category

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | | Mount Whitney | bold=1}}',

'Mount Whitney')

-- known category with categorytext

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | | Mount Whitney |linktext=Big Mountain|bold=1|italic=1}}',

'Big Mountain')

-- known link and category

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | Mount Whitney | Mount Whitney}}',

'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}}',

'Big Mountain (category)')

-- both gallery and category exist

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | qid=Q816044|bold=1|categoryText=cat}}',

'Bellows Falls, Vermont (cat)')

-- P373 matches sitelink, category only

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | qid=Q2601154|bold=1}}',

'Agama lionotus')

-- P910 matches P373, category only

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | qid=Q853037|italic=1|bold=1}}',

'Bánhida')

-- P1754 matches P373, category only

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | qid=Q2551907}}',

'Lakes of Turkey')

-- missing data

self:preprocess_equals('{{#invoke:Commons link/sandbox| getGalleryAndCategory | qid=Q2963317|bold=1|linktext=Chicago Lawn|oneSearch=1}}',

'Chicago Lawn')

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