-- @copyright (c) 2016 Bourdercloud.com -- @author Karima Rafes -- @link http://www.mediawiki.org/wiki/Extension:LinkedWiki -- @license CC-by-nc-sa V3.0 -- -- Last version : http://github.com/BorderCloud/LinkedWiki -- -- -- This work is licensed under the Creative Commons -- Attribution-NonCommercial-ShareAlike 3.0 -- Unported License. To view a copy of this license, -- visit http://creativecommons.org/licenses/by-nc-sa/3.0/ -- or send a letter to Creative Commons, -- 171 Second Street, Suite 300, San Francisco, -- California, 94105, USA. -- @copyright (c) 2016 Bourdercloud.com -- @author Karima Rafes -- @link http://www.mediawiki.org/wiki/Extension:LinkedWiki -- @license CC-by-nc-sa V3.0 -- -- Last version : http://github.com/BorderCloud/LinkedWiki -- -- -- This work is licensed under the Creative Commons -- Attribution-NonCommercial-ShareAlike 3.0 -- Unported License. To view a copy of this license, -- visit http://creativecommons.org/licenses/by-nc-sa/3.0/ -- or send a letter to Creative Commons, -- 171 Second Street, Suite 300, San Francisco, -- California, 94105, USA. --[[ -- Debug console mw.log(p.tests() ) ]] local p = {} function p.checkLitteral(query, litteral) local result = '' if string.match(query, litteral) then result = "OK" else result = "KO" end return result end function p.checkString(val1, val2) local result = '' if (val1 == nil or val2 == nil) then result = "KO1" else if val1 == val2 then result = "OK" else result = "KO2" end end return result end function p.checkNumber(val1, val2) local result = '' if (val1 == nil or val2 == nil) then result = "KO" else if tonumber(val1) == tonumber(val2) then result = "OK" else result = "KO" end end return result end function p.tests(f) local linkedwiki = require 'linkedwiki' local endpoint = 'http://database-test:8890/sparql' local config = 'http://database-test' local xsd = 'http://www.w3.org/2001/XMLSchema#' local rdfs = 'http://www.w3.org/2000/01/rdf-schema#' local pr = 'http://database-test/TestFunction:' local html = '== TESTS =='.. '\n' linkedwiki.setDebug(true) html = html .."TEST : linkedwiki.getCurrentIRI()" .. '\n' html = html .."RESULT : " .. linkedwiki.getCurrentIRI() .. '\n' local subject = linkedwiki.getCurrentIRI() local objTest = linkedwiki.new(subject,config) local pTObjTemp1 = mw.title.new("Title1"):fullUrl() local pTObjTemp2 = mw.title.new("Title2"):fullUrl() local ObjTemp1 = linkedwiki.new(pTObjTemp1); local ObjTemp2 = linkedwiki.new(pTObjTemp2); mw.log(objTest:removeSubject()) html = html .."----------------------------------------------------------------------------" ..'\n' html = html .."TEST : checkValue" ..'\n' -- function linkedwiki.checkValue(property, valueInWiki) local valueInWiki = 0 local result = "" local pT ="" pT = pr..'1' valueInWiki = "1" result = objTest:checkValue(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
1
') ..'\n' mw.log(objTest:addPropertyWithLitteral(pT,1)) valueInWiki = "1" result = objTest:checkValue(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
1
') ..'\n' valueInWiki = "2" result =objTest:checkValue(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
2
') ..'\n' html = html .."----------------------------------------------------------------------------" ..'\n' html = html .."TEST : checkString" ..'\n' -- function objTest:checkString(property, valueInWiki, tagLang) pT = pr..'2' valueInWiki = "test" result = objTest:checkString(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
test
') ..'\n' --mw.log(objTest:addPropertyWithLitteral(pT,"test")) mw.log(objTest:addPropertyString(pT,"test")) valueInWiki = "test" result = objTest:checkString(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
test
') ..'\n' valueInWiki = "testDifferent" result = objTest:checkString(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
testDifferent
') ..'\n' --mw.log(objTest:addPropertyWithLitteral(pT,"testfr",nil,"fr")) mw.log(objTest:addPropertyString(pT,"testfr","fr")) valueInWiki = "testfr" result = objTest:checkString(pT,valueInWiki,"fr") html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" .. '\n' html = html .."RESULT : " .. p.checkString(result,'
testfr
') ..'\n' valueInWiki = "testfrDifferent" result = objTest:checkString(pT,valueInWiki,"fr") html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
testfrDifferent
') ..'\n' html = html .."----------------------------------------------------------------------------" ..'\n' html = html .."TEST : checkLabelOfInternLink" ..'\n' -- function objTest:checkLabelOfInternLink(link, propertyOfLabel, labelInWiki, tagLang) pT = pr..'3' valueInWiki = "test" local link = "http://example.com/link" result = objTest:checkLabelOfInternLink(link,pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[http://example.com/link test]
') ..'\n' mw.log(objTest:addPropertyWithLitteral(pT,"test")) valueInWiki = "test" result = objTest:checkLabelOfInternLink(link,pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[http://example.com/link test]
') ..'\n' valueInWiki = "test2" result = objTest:checkLabelOfInternLink(link,pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[http://example.com/link test2]
') ..'\n' html = html .."----------------------------------------------------------------------------" ..'\n' html = html .."TEST : checkIriOfExternLink" ..'\n' -- function objTest:checkIriOfExternLink(labelOfExternLink, propertyOfExternLink, externLinkInWiki) pT = pr..'4' valueInWiki = "http://example.com/test" result = objTest:checkIriOfExternLink("test",pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[http://example.com/test test]
') ..'\n' mw.log(objTest:addPropertyWithIri(pT,"http://example.com/test")) valueInWiki = "http://example.com/test" result = objTest:checkIriOfExternLink("test",pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[http://example.com/test test]
') ..'\n' valueInWiki = "http://example.com/test2" result = objTest:checkIriOfExternLink("test",pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[http://example.com/test2 test]
') ..'\n' html = html .."----------------------------------------------------------------------------" ..'\n' html = html .."TEST : checkImage" ..'\n' -- function objTest:checkImage(property, valueInWiki, width, height) pT = pr..'5' valueInWiki = "http://example.com/test.png" result = objTest:checkImage(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
') ..'\n' mw.log(objTest:addPropertyWithIri(pT,"http://example.com/test.png")) valueInWiki = "http://example.com/test.png" result = objTest:checkImage(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
') ..'\n' valueInWiki = "http://example.com/test2.png" result = objTest:checkImage(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
') ..'\n' html = html .."----------------------------------------------------------------------------" ..'\n' html = html .."TEST : checkTitle" ..'\n' -- objTest:checkTitle(property, labelInWiki, tagLang) objTest:setDebug(true) pT = pr..'6' local pTObjTemp1 = mw.title.new("Title1"):fullUrl() local pTObjTemp2 = mw.title.new("Title2"):fullUrl() local ObjTemp1 = linkedwiki.new(pTObjTemp1,config); local ObjTemp2 = linkedwiki.new(pTObjTemp2,config); -- mw.log(objTest:getConfig()) -- mw.log(ObjTemp1:getConfig()) -- mw.log(ObjTemp2:getConfig()) mw.log(objTest:removeSubject()) mw.log(ObjTemp1:removeSubject()) mw.log(ObjTemp2:removeSubject()) valueInWiki = "" --0 in DB result = objTest:checkTitle(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'') ..'\n' valueInWiki = "Title1;Title2" --0 in DB result = objTest:checkTitle(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[[Title1]], [[Title2]]
') ..'\n' valueInWiki = " Title1;Title2 " --"Title1" in DB -- current - pT -> pTObj -- pTObj - rdfs:label -> "Title1" mw.log(objTest:addPropertyWithIri(pT, pTObjTemp1)) --mw.log(ObjTemp1:addPropertyWithLitteral(rdfs.."label","Title1")) mw.log(ObjTemp1:addPropertyString(rdfs.."label","Title1")) -- mw.log(linkedwiki.getLastQuery()) result = objTest:checkTitle(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[[Title2]][http://wiki.serverdev-mediawiki-v1/index.php/Title1 Title1]
') ..'\n' valueInWiki = "Title1 ; Title2" --"Title1;Title2" in DB --"Title1" in DB -- current - pT -> pTObj2 -- pTObj2 - rdfs:label -> "Title2" mw.log(objTest:addPropertyWithIri(pT,pTObjTemp2)) --mw.log(objTest:addPropertyWithLitteral(rdfs.."label","Title2",nil,nil,pTObj2)) mw.log(ObjTemp2:addPropertyString(rdfs.."label","Title2")) result = objTest:checkTitle(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[http://wiki.serverdev-mediawiki-v1/index.php/Title1 Title1], [http://wiki.serverdev-mediawiki-v1/index.php/Title2 Title2]
') ..'\n' valueInWiki = "" --"Title1;Title2" in DB result = objTest:checkTitle(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[http://wiki.serverdev-mediawiki-v1/index.php/Title1 Title1], [http://wiki.serverdev-mediawiki-v1/index.php/Title2 Title2]
') ..'\n' html = html .."----------------------------------------------------------------------------" ..'\n' html = html .."TEST : checkUser" ..'\n' -- function objTest:checkUser(property, valueInWiki) mw.log(objTest:removeSubject()) pT = pr..'7' local pTObjTemp1 = mw.title.new("User:Firstname Surename1"):fullUrl() local pTObjTemp2 = mw.title.new("User:Firstname Surename2"):fullUrl() local ObjTemp1 = linkedwiki.new(pTObjTemp1,config) local ObjTemp2 = linkedwiki.new(pTObjTemp2,config) mw.log(objTest:removeSubject()) mw.log(ObjTemp1:removeSubject()) mw.log(ObjTemp2:removeSubject()) valueInWiki = "" --0 in DB result = objTest:checkUser(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'') ..'\n' valueInWiki = "User1;User2" --0 in DB result = objTest:checkUser(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[[User:User1|User1]], [[User:User2|User2]]
') ..'\n' valueInWiki = "Firstname Surename1; Firstname Surename2" --"User:Firstname Surename1" in DB -- current - pT -> pTObj -- pTObj - rdfs:label -> "User:Firstname Surename1" mw.log(objTest:addPropertyWithIri(pT, pTObjTemp1)) mw.log(ObjTemp1:addPropertyWithLitteral(rdfs.."label","Firstname Surename1")) result = objTest:checkUser(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[[User:Firstname Surename2|Firstname Surename2]][http://wiki.serverdev-mediawiki-v1/index.php/User:Firstname_Surename1 Firstname Surename1]
') ..'\n' valueInWiki = "Firstname Surename1; Firstname Surename2" --"User:Firstname Surename1;User:Firstname Surename2" in DB --"User:Firstname Surename2" in DB -- current - pT -> pTObj2 -- pTObj2 - rdfs:label -> "User:Firstname Surename2" mw.log(objTest:addPropertyWithIri(pT,pTObjTemp2)) mw.log(ObjTemp2:addPropertyWithLitteral(rdfs.."label","Firstname Surename2")) result = objTest:checkUser(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[http://wiki.serverdev-mediawiki-v1/index.php/User:Firstname_Surename1 Firstname Surename1], [http://wiki.serverdev-mediawiki-v1/index.php/User:Firstname_Surename2 Firstname Surename2]
') ..'\n' valueInWiki = "Firstname Surename1; Firstname Surename2" --"User:Firstname Surename1;User:Firstname Surename2" in DB --"mailto:Firstname_Surename2@example.com" ADD in DB -- current - pT -> pTObj2 -- pTObj2 - http://www.w3.org/2006/vcard/ns#email -> "mailto:Firstname_Surename2@example.com" mw.log(ObjTemp2:addPropertyWithIri("http://www.w3.org/2006/vcard/ns#email","mailto:Firstname_Surename2@example.com")) result = objTest:checkUser(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[http://wiki.serverdev-mediawiki-v1/index.php/User:Firstname_Surename1 Firstname Surename1], [http://wiki.serverdev-mediawiki-v1/index.php/User:Firstname_Surename2 Firstname Surename2][mailto:Firstname_Surename2@example.com ✉]
') ..'\n' html = html .."RESULT BEGIN : "..'\n' ..ObjTemp2:getValue("http://www.w3.org/2006/vcard/ns#email") ..'\n'.."END" ..'\n' valueInWiki = "Firstname Surename1; Firstname Surename2" --"User:Firstname Surename1;User:Firstname Surename2" in DB --"mailto:Firstname_Surename2@example.com" ADD in DB -- current - pT -> pTObj2 -- pTObj2 - http://www.w3.org/2006/vcard/ns#email -> "mailto:Firstname_Surename2@example.com" mw.log(ObjTemp2:addPropertyWithIri("http://www.w3.org/2006/vcard/ns#email","mailto:Firstname_Surename2Bis@example.com")) result = objTest:checkUser(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[http://wiki.serverdev-mediawiki-v1/index.php/User:Firstname_Surename1 Firstname Surename1], [http://wiki.serverdev-mediawiki-v1/index.php/User:Firstname_Surename2 Firstname Surename2][mailto:Firstname_Surename2@example.com ✉][mailto:Firstname_Surename2Bis@example.com ✉]
') ..'\n' valueInWiki = "" --"Title1;Title2" in DB result = objTest:checkUser(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[http://wiki.serverdev-mediawiki-v1/index.php/User:Firstname_Surename1 Firstname Surename1], [http://wiki.serverdev-mediawiki-v1/index.php/User:Firstname_Surename2 Firstname Surename2][mailto:Firstname_Surename2@example.com ✉][mailto:Firstname_Surename2Bis@example.com ✉]
') ..'\n' html = html .."----------------------------------------------------------------------------" ..'\n' html = html .."TEST : checkItem" ..'\n' -- function objTest:checkItem(property, valueInWiki, tagLang) mw.log(objTest:removeSubject()) objTest:setDebug(true) pT = pr..'8' local wd = "http://www.wikidata.org/entity/" local pTObjTemp1 = wd.."Q1" local pTObjTemp2 = wd.."Q2" local ObjTemp1 = linkedwiki.new(pTObjTemp1,config) local ObjTemp2 = linkedwiki.new(pTObjTemp2,config) mw.log(objTest:removeSubject()) mw.log(ObjTemp1:removeSubject()) mw.log(ObjTemp2:removeSubject()) valueInWiki = "" --0 in DB result = objTest:checkItem(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'') ..'\n' valueInWiki = "Q1 ; Q2 " --0 in DB result = objTest:checkItem(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[[https://www.wikidata.org/wiki/Special:GoToLinkedPage/enwiki/Q2 Earth]([http://www.wikidata.org/entity/Q2 Q2]), [[https://www.wikidata.org/wiki/Special:GoToLinkedPage/enwiki/Q1 universe]([http://www.wikidata.org/entity/Q1 Q1])
') ..'\n' valueInWiki = "Q1;Q2" --"Title1" in DB -- current - pT -> pTObj -- pTObj - rdfs:label -> "universe" mw.log(objTest:addPropertyWithIri(pT, pTObjTemp1)) mw.log(ObjTemp1:addPropertyWithLitteral(rdfs.."label","universe")) result = objTest:checkItem(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[[https://www.wikidata.org/wiki/Special:GoToLinkedPage/enwiki/Q2 Earth]([http://www.wikidata.org/entity/Q2 Q2])[[https://www.wikidata.org/wiki/Special:GoToLinkedPage/enwiki/Q1 universe]([http://www.wikidata.org/entity/Q1 Q1])
') ..'\n' valueInWiki = " Q1 ; Q2 " --"Title1;Title2" in DB --"Title1" in DB -- current - pT -> pTObj2 -- pTObj2 - rdfs:label -> "Title2" mw.log(objTest:addPropertyWithIri(pT,pTObjTemp2)) mw.log(ObjTemp2:addPropertyWithLitteral(rdfs.."label","Earth")) result = objTest:checkItem(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[[https://www.wikidata.org/wiki/Special:GoToLinkedPage/enwiki/Q2 Earth]([http://www.wikidata.org/entity/Q2 Q2]), [[https://www.wikidata.org/wiki/Special:GoToLinkedPage/enwiki/Q1 universe]([http://www.wikidata.org/entity/Q1 Q1])
') ..'\n' valueInWiki = "" --"Title1;Title2" in DB result = objTest:checkItem(pT,valueInWiki) html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
[[https://www.wikidata.org/wiki/Special:GoToLinkedPage/enwiki/Q2 Earth]([http://www.wikidata.org/entity/Q2 Q2]), [[https://www.wikidata.org/wiki/Special:GoToLinkedPage/enwiki/Q1 universe]([http://www.wikidata.org/entity/Q1 Q1])
') ..'\n' html = html .."----------------------------------------------------------------------------" ..'\n' html = html .."TEST : printDateInWiki" ..'\n' --Linkedwiki:printDateInWiki(format, valueInWiki, valueInDB) local dateFormat = "d M Y" -- {{#time:d M Y|2004-12-06}} --"2004-12-06"^^xsd..'date' result = objTest:printDateInWiki(dateFormat, "2004-12-06", "2004-12-06") html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
{{#time:d M Y|2004-12-06}}
') ..'\n' result = objTest:printDateInWiki(dateFormat, "2004-12-07", "2004-12-06") html = html .."RESULT BEGIN : "..'\n' ..result ..'\n'.."END" ..'\n' html = html .."RESULT : " .. p.checkString(result,'
{{#time:d M Y|2004-12-07}}
') ..'\n' objTest:addProperty('http://example.com/deces',"2004-12-06",xsd..'date') -- mw.log(linkedwiki.getLastQuery()) mw.log(objTest:removeSubject()) --return html return "
"..mw.text.encode( html).."
" end return p