jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/394610 )

Change subject: Hygiene: Add media selector unit tests
......................................................................


Hygiene: Add media selector unit tests

Bug: T177430
Change-Id: I96fe3b9ea38d5721f8f85cb887935bd31a58fd21
---
M lib/media.js
M routes/media.js
A test/lib/media/media-test.js
3 files changed, 69 insertions(+), 9 deletions(-)

Approvals:
  BearND: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/media.js b/lib/media.js
index 4d89149..270b799 100644
--- a/lib/media.js
+++ b/lib/media.js
@@ -1,5 +1,6 @@
 'use strict';
 
+const domino = require('domino');
 const api = require('./api-util');
 const mUtil = require('./mobile-util');
 const Title = require('mediawiki-title').Title;
@@ -51,11 +52,14 @@
 
 /**
  * Get file page titles from a NodeList of media elements from Parsoid HTML
- * @param {!NodeList} selection NodeList containing media items from Parsoid 
HTML
+ * @param {!String} html raw Parsoid HTML
  * @return {!Array} array containing the information on the media items on the 
page, in order of
  *          appearance
  */
-function getMediaItemInfoFromPage(selection) {
+function getMediaItemInfoFromPage(html) {
+    const doc = domino.createDocument(html);
+    // todo: handle Mathoid-rendered math images
+    const selection = doc.querySelectorAll(SELECTORS.join(','));
     return [].map.call(selection, (elem) => {
         const mediaType = getMediaType(elem.getAttribute('typeof'));
         const resourceElem = elem.querySelector(mediaType.resourceSelector);
@@ -203,5 +207,8 @@
     getMediaItemInfoFromPage,
     getMetadataFromApi,
     filterResult,
-    SELECTORS
+    SELECTORS,
+    Image,
+    Video,
+    Audio
 };
diff --git a/routes/media.js b/routes/media.js
index db34514..8c0f56f 100644
--- a/routes/media.js
+++ b/routes/media.js
@@ -1,7 +1,6 @@
 'use strict';
 
 const BBPromise = require('bluebird');
-const domino = require('domino');
 const mUtil = require('../lib/mobile-util');
 const parsoid = require('../lib/parsoid-access');
 const sUtil = require('../lib/util');
@@ -21,14 +20,11 @@
         siteinfo: mwapi.getSiteInfo(app, req)
     }).then((response) => {
         const revTid = parsoid.getRevAndTidFromEtag(response.html.headers);
-        const doc = domino.createDocument(response.html.body);
-        // todo: handle Mathoid-rendered math images
-        const selection = doc.querySelectorAll(media.SELECTORS.join(','));
-        if (!selection) {
+        const mediaList = media.getMediaItemInfoFromPage(response.html.body);
+        if (!mediaList.length) {
             res.send({ items: [] });
             return;
         }
-        const mediaList = media.getMediaItemInfoFromPage(selection);
         const titles = mUtil.deduplicate(mediaList.map(item => item.title));
         return media.getMetadataFromApi(app, req, titles, 
response.siteinfo).then((response) => {
             mUtil.mergeByProp(mediaList, response.items, 'title', false);
diff --git a/test/lib/media/media-test.js b/test/lib/media/media-test.js
new file mode 100644
index 0000000..9417069
--- /dev/null
+++ b/test/lib/media/media-test.js
@@ -0,0 +1,57 @@
+/* eslint-disable max-len */
+
+'use strict';
+
+const assert = require('../../utils/assert');
+const media = require('../../../lib/media');
+
+const imageFigure = '<figure typeof="mw:Image"><img 
resource="./File:Foo"/></figure>';
+const imageSpan = '<span typeof="mw:Image"><img 
resource="./File:Foo"/></span>';
+const imageFigureInline = '<figure-inline typeof="mw:Image"><img 
resource="./File:Foo"/></figure-inline>';
+
+const imageThumbFigure = '<figure typeof="mw:Image/Thumb"><img 
resource="./File:Foo"/></figure>';
+const imageThumbSpan = '<span typeof="mw:Image/Thumb"><img 
resource="./File:Foo"/></span>';
+const imageThumbFigureInline = '<figure-inline typeof="mw:Image/Thumb"><img 
resource="./File:Foo"/></figure-inline>';
+
+const videoFigure = '<figure typeof="mw:Video"><video 
resource="./File:Foo"/></figure>';
+const videoSpan = '<span typeof="mw:Video"><video 
resource="./File:Foo"/></span>';
+const videoFigureInline = '<figure-inline typeof="mw:Video"><video 
resource="./File:Foo"/></figure-inline>';
+
+const videoThumbFigure = '<figure typeof="mw:Video/Thumb"><video 
resource="./File:Foo"/></figure>';
+const videoThumbSpan = '<span typeof="mw:Video/Thumb"><video 
resource="./File:Foo"/></span>';
+const videoThumbFigureInline = '<figure-inline typeof="mw:Video/Thumb"><video 
resource="./File:Foo"/></figure-inline>';
+
+const audioFigure = '<figure typeof="mw:Audio"><video 
resource="./File:Foo"/></figure>';
+const audioSpan = '<span typeof="mw:Audio"><video 
resource="./File:Foo"/></span>';
+const audioFigureInline = '<figure-inline typeof="mw:Audio"><video 
resource="./File:Foo"/></figure-inline>';
+
+const noTypeFigure = '<figure><video resource="./File:Foo"/></figure>';
+const noTypeSpan = '<span><video resource="./File:Foo"/></span>';
+const noTypeFigureInline = '<figure-inline><video 
resource="./File:Foo"/></figure-inline>';
+
+const images = [imageFigure, imageSpan, imageFigureInline, imageThumbFigure, 
imageThumbSpan, imageThumbFigureInline];
+const videos = [videoFigure, videoSpan, videoFigureInline, videoThumbFigure, 
videoThumbSpan, videoThumbFigureInline];
+const audio = [audioFigure, audioSpan, audioFigureInline];
+
+const validItems = images.concat(videos).concat(audio);
+const invalidItems = [noTypeFigure, noTypeSpan, noTypeFigureInline];
+
+describe('lib:media', () => {
+
+    it('items should be found for expected selectors', () => {
+        const page = validItems.join('');
+        const result = media.getMediaItemInfoFromPage(page);
+        assert.deepEqual(result.length, validItems.length);
+        assert.deepEqual(result.filter(i => i.title === 'File:Foo').length, 
validItems.length);
+        assert.deepEqual(result.filter(i => i.type === 
media.Image.name).length, images.length);
+        assert.deepEqual(result.filter(i => i.type === 
media.Video.name).length, videos.length);
+        assert.deepEqual(result.filter(i => i.type === 
media.Audio.name).length, audio.length);
+    });
+
+    it('items should not be found for other selectors', () => {
+        const page = invalidItems.join('');
+        const result = media.getMediaItemInfoFromPage(page);
+        assert.deepEqual(result.length, 0);
+    });
+
+});

-- 
To view, visit https://gerrit.wikimedia.org/r/394610
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I96fe3b9ea38d5721f8f85cb887935bd31a58fd21
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: Mholloway <mhollo...@wikimedia.org>
Gerrit-Reviewer: BearND <bsitzm...@wikimedia.org>
Gerrit-Reviewer: Fjalapeno <cfl...@wikimedia.org>
Gerrit-Reviewer: Jdlrobson <jrob...@wikimedia.org>
Gerrit-Reviewer: Mhurd <mh...@wikimedia.org>
Gerrit-Reviewer: Ppchelko <ppche...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to