From 1305a10ee0cce140f9e92092126990cae9e11314 Mon Sep 17 00:00:00 2001 From: Jonathan Labreuille Date: Fri, 5 Aug 2016 15:25:54 +0200 Subject: [PATCH 1/2] updated ALC endpoint + nl editor + js tests --- .../blocks/automatedLatestContent.js | 16 ++--- .../js/src/newsletter_editor/blocks/posts.js | 24 +++---- .../components/communication.js | 4 +- lib/API/Endpoint.php | 1 - lib/API/Endpoints/AutomatedLatestContent.php | 63 +++++++++++-------- lib/API/SuccessResponse.php | 10 +-- .../blocks/automatedLatestContent.spec.js | 2 +- .../components/communication.spec.js | 6 +- 8 files changed, 70 insertions(+), 56 deletions(-) diff --git a/assets/js/src/newsletter_editor/blocks/automatedLatestContent.js b/assets/js/src/newsletter_editor/blocks/automatedLatestContent.js index 8c99698ee7..87835ac05a 100644 --- a/assets/js/src/newsletter_editor/blocks/automatedLatestContent.js +++ b/assets/js/src/newsletter_editor/blocks/automatedLatestContent.js @@ -51,9 +51,9 @@ define([ blocks: blocks, }).then(_.partial(this.refreshBlocks, models)); }, - refreshBlocks: function(models, renderedBlocks) { + refreshBlocks: function(models, response) { _.each( - _.zip(models, renderedBlocks), + _.zip(models, response.data), function(args) { var model = args[0], contents = args[1]; @@ -217,17 +217,19 @@ define([ }; }, transport: function(options, success, failure) { - var taxonomies, - promise = CommunicationComponent.getTaxonomies(that.model.get('contentType')).then(function(tax) { - taxonomies = tax; + var taxonomies; + var promise = CommunicationComponent.getTaxonomies( + that.model.get('contentType') + ).then(function(response) { + taxonomies = response.data; // Fetch available terms based on the list of taxonomies already fetched var promise = CommunicationComponent.getTerms({ search: options.data.term, taxonomies: _.keys(taxonomies) - }).then(function(terms) { + }).then(function(response) { return { taxonomies: taxonomies, - terms: terms, + terms: response.data }; }); return promise; diff --git a/assets/js/src/newsletter_editor/blocks/posts.js b/assets/js/src/newsletter_editor/blocks/posts.js index 3062e803d1..f14c32d984 100644 --- a/assets/js/src/newsletter_editor/blocks/posts.js +++ b/assets/js/src/newsletter_editor/blocks/posts.js @@ -108,8 +108,8 @@ define([ }, fetchAvailablePosts: function() { var that = this; - CommunicationComponent.getPosts(this.toJSON()).done(function(posts) { - that.get('_availablePosts').reset(posts); + CommunicationComponent.getPosts(this.toJSON()).done(function(response) { + that.get('_availablePosts').reset(response.data); that.get('_selectedPosts').reset(); // Empty out the collection that.trigger('change:_availablePosts'); }).fail(function() { @@ -127,8 +127,8 @@ define([ return; } - CommunicationComponent.getTransformedPosts(data).done(function(posts) { - that.get('_transformedPosts').get('blocks').reset(posts, {parse: true}); + CommunicationComponent.getTransformedPosts(data).done(function(response) { + that.get('_transformedPosts').get('blocks').reset(response.data, {parse: true}); }).fail(function() { MailPoet.Notice.error(MailPoet.I18n.t('failedToFetchRenderedPosts')); }); @@ -143,8 +143,8 @@ define([ if (data.posts.length === 0) return; - CommunicationComponent.getTransformedPosts(data).done(function(posts) { - collection.add(posts, { at: index }); + CommunicationComponent.getTransformedPosts(data).done(function(response) { + collection.add(response.data, { at: index }); }).fail(function() { MailPoet.Notice.error(MailPoet.I18n.t('failedToFetchRenderedPosts')); }); @@ -302,17 +302,19 @@ define([ }; }, transport: function(options, success, failure) { - var taxonomies, - promise = CommunicationComponent.getTaxonomies(that.model.get('contentType')).then(function(tax) { - taxonomies = tax; + var taxonomies; + var promise = CommunicationComponent.getTaxonomies( + that.model.get('contentType') + ).then(function(response) { + taxonomies = response.data; // Fetch available terms based on the list of taxonomies already fetched var promise = CommunicationComponent.getTerms({ search: options.data.term, taxonomies: _.keys(taxonomies) - }).then(function(terms) { + }).then(function(response) { return { taxonomies: taxonomies, - terms: terms, + terms: response.data }; }); return promise; diff --git a/assets/js/src/newsletter_editor/components/communication.js b/assets/js/src/newsletter_editor/components/communication.js index 7792425f6e..439001f525 100644 --- a/assets/js/src/newsletter_editor/components/communication.js +++ b/assets/js/src/newsletter_editor/components/communication.js @@ -27,8 +27,8 @@ define([ return Module._cachedQuery({ action: 'getPostTypes', options: {}, - }).then(function(types) { - return _.values(types); + }).then(function(response) { + return _.values(response.data); }); }; diff --git a/lib/API/Endpoint.php b/lib/API/Endpoint.php index 4cc419c481..874514f6ea 100644 --- a/lib/API/Endpoint.php +++ b/lib/API/Endpoint.php @@ -8,7 +8,6 @@ abstract class Endpoint { function successResponse( $data = array(), $meta = array(), $status = Response::STATUS_OK ) { - return new SuccessResponse($data, $meta, $status); } diff --git a/lib/API/Endpoints/AutomatedLatestContent.php b/lib/API/Endpoints/AutomatedLatestContent.php index ffe9ee1bec..f866a91637 100644 --- a/lib/API/Endpoints/AutomatedLatestContent.php +++ b/lib/API/Endpoints/AutomatedLatestContent.php @@ -1,9 +1,11 @@ successResponse( + get_post_types(array(), 'objects') + ); } - function getTaxonomies($args) { - $post_type = (isset($args['postType'])) ? $args['postType'] : 'post'; - return get_object_taxonomies($post_type, 'objects'); + function getTaxonomies($data = array()) { + $post_type = (isset($data['postType'])) ? $data['postType'] : 'post'; + return $this->successResponse( + get_object_taxonomies($post_type, 'objects') + ); } - function getTerms($args) { - $taxonomies = (isset($args['taxonomies'])) ? $args['taxonomies'] : array(); - $search = (isset($args['search'])) ? $args['search'] : ''; - $limit = (isset($args['limit'])) ? (int)$args['limit'] : 10; - $page = (isset($args['page'])) ? (int)$args['page'] : 1; - return get_terms( - $taxonomies, - array( - 'hide_empty' => false, - 'search' => $search, - 'number' => $limit, - 'offset' => $limit * ($page - 1) + function getTerms($data = array()) { + $taxonomies = (isset($data['taxonomies'])) ? $data['taxonomies'] : array(); + $search = (isset($data['search'])) ? $data['search'] : ''; + $limit = (isset($data['limit'])) ? (int)$data['limit'] : 10; + $page = (isset($data['page'])) ? (int)$data['page'] : 1; + + return $this->successResponse( + get_terms( + $taxonomies, + array( + 'hide_empty' => false, + 'search' => $search, + 'number' => $limit, + 'offset' => $limit * ($page - 1) + ) ) ); } - function getPosts($args) { - return $this->ALC->getPosts($args); + function getPosts($data = array()) { + return $this->successResponse( + $this->ALC->getPosts($data) + ); } - function getTransformedPosts($args) { - $posts = $this->ALC->getPosts($args); - return $this->ALC->transformPosts($args, $posts); + function getTransformedPosts($data = array()) { + $posts = $this->ALC->getPosts($data); + return $this->successResponse( + $this->ALC->transformPosts($data, $posts) + ); } - function getBulkTransformedPosts($args) { + function getBulkTransformedPosts($data = array()) { $alc = new \MailPoet\Newsletter\AutomatedLatestContent(); $used_posts = array(); $rendered_posts = array(); - foreach($args['blocks'] as $block) { + foreach($data['blocks'] as $block) { $posts = $alc->getPosts($block, $used_posts); $rendered_posts[] = $alc->transformPosts($block, $posts); @@ -59,6 +72,6 @@ class AutomatedLatestContent { } } - return $rendered_posts; + return $this->successResponse($rendered_posts); } } diff --git a/lib/API/SuccessResponse.php b/lib/API/SuccessResponse.php index 251774c173..35c2134741 100644 --- a/lib/API/SuccessResponse.php +++ b/lib/API/SuccessResponse.php @@ -12,12 +12,8 @@ class SuccessResponse extends Response { } function getData() { - if(empty($this->data)) { - return false; - } else { - return array( - 'data' => $this->data - ); - } + return array( + 'data' => $this->data + ); } } \ No newline at end of file diff --git a/tests/javascript/newsletter_editor/blocks/automatedLatestContent.spec.js b/tests/javascript/newsletter_editor/blocks/automatedLatestContent.spec.js index 30baad156f..1b65febd1d 100644 --- a/tests/javascript/newsletter_editor/blocks/automatedLatestContent.spec.js +++ b/tests/javascript/newsletter_editor/blocks/automatedLatestContent.spec.js @@ -53,7 +53,7 @@ define([ mock1.expects('trigger').once().withArgs('refreshPosts', postsSet1); mock2.expects('trigger').once().withArgs('refreshPosts', postsSet2); - model.refreshBlocks([block1, block2], [postsSet1, postsSet2]); + model.refreshBlocks([block1, block2], { data: [postsSet1, postsSet2] }); mock1.verify(); mock2.verify(); diff --git a/tests/javascript/newsletter_editor/components/communication.spec.js b/tests/javascript/newsletter_editor/components/communication.spec.js index d1d423a247..a2549e7615 100644 --- a/tests/javascript/newsletter_editor/components/communication.spec.js +++ b/tests/javascript/newsletter_editor/components/communication.spec.js @@ -12,8 +12,10 @@ define([ post: function() { var deferred = jQuery.Deferred(); deferred.resolve({ - 'post': 'val1', - 'page': 'val2', + data: { + 'post': 'val1', + 'page': 'val2', + } }); return deferred; } From c136d91dd2c3bf94bfa16114e3d5128b38323340 Mon Sep 17 00:00:00 2001 From: Jonathan Labreuille Date: Mon, 8 Aug 2016 11:36:29 +0200 Subject: [PATCH 2/2] encapsulating in communication component + update js tests --- .../blocks/automatedLatestContent.js | 12 +++--- .../js/src/newsletter_editor/blocks/posts.js | 20 ++++----- .../components/communication.js | 22 +++++++--- .../blocks/automatedLatestContent.spec.js | 2 +- .../components/communication.spec.js | 43 +++++++++++++------ 5 files changed, 64 insertions(+), 35 deletions(-) diff --git a/assets/js/src/newsletter_editor/blocks/automatedLatestContent.js b/assets/js/src/newsletter_editor/blocks/automatedLatestContent.js index 87835ac05a..8177d59872 100644 --- a/assets/js/src/newsletter_editor/blocks/automatedLatestContent.js +++ b/assets/js/src/newsletter_editor/blocks/automatedLatestContent.js @@ -51,9 +51,9 @@ define([ blocks: blocks, }).then(_.partial(this.refreshBlocks, models)); }, - refreshBlocks: function(models, response) { + refreshBlocks: function(models, renderedBlocks) { _.each( - _.zip(models, response.data), + _.zip(models, renderedBlocks), function(args) { var model = args[0], contents = args[1]; @@ -220,16 +220,16 @@ define([ var taxonomies; var promise = CommunicationComponent.getTaxonomies( that.model.get('contentType') - ).then(function(response) { - taxonomies = response.data; + ).then(function(tax) { + taxonomies = tax; // Fetch available terms based on the list of taxonomies already fetched var promise = CommunicationComponent.getTerms({ search: options.data.term, taxonomies: _.keys(taxonomies) - }).then(function(response) { + }).then(function(terms) { return { taxonomies: taxonomies, - terms: response.data + terms: terms }; }); return promise; diff --git a/assets/js/src/newsletter_editor/blocks/posts.js b/assets/js/src/newsletter_editor/blocks/posts.js index f14c32d984..113cfa93e4 100644 --- a/assets/js/src/newsletter_editor/blocks/posts.js +++ b/assets/js/src/newsletter_editor/blocks/posts.js @@ -108,8 +108,8 @@ define([ }, fetchAvailablePosts: function() { var that = this; - CommunicationComponent.getPosts(this.toJSON()).done(function(response) { - that.get('_availablePosts').reset(response.data); + CommunicationComponent.getPosts(this.toJSON()).done(function(posts) { + that.get('_availablePosts').reset(posts); that.get('_selectedPosts').reset(); // Empty out the collection that.trigger('change:_availablePosts'); }).fail(function() { @@ -127,8 +127,8 @@ define([ return; } - CommunicationComponent.getTransformedPosts(data).done(function(response) { - that.get('_transformedPosts').get('blocks').reset(response.data, {parse: true}); + CommunicationComponent.getTransformedPosts(data).done(function(posts) { + that.get('_transformedPosts').get('blocks').reset(posts, {parse: true}); }).fail(function() { MailPoet.Notice.error(MailPoet.I18n.t('failedToFetchRenderedPosts')); }); @@ -143,8 +143,8 @@ define([ if (data.posts.length === 0) return; - CommunicationComponent.getTransformedPosts(data).done(function(response) { - collection.add(response.data, { at: index }); + CommunicationComponent.getTransformedPosts(data).done(function(posts) { + collection.add(posts, { at: index }); }).fail(function() { MailPoet.Notice.error(MailPoet.I18n.t('failedToFetchRenderedPosts')); }); @@ -305,16 +305,16 @@ define([ var taxonomies; var promise = CommunicationComponent.getTaxonomies( that.model.get('contentType') - ).then(function(response) { - taxonomies = response.data; + ).then(function(tax) { + taxonomies = tax; // Fetch available terms based on the list of taxonomies already fetched var promise = CommunicationComponent.getTerms({ search: options.data.term, taxonomies: _.keys(taxonomies) - }).then(function(response) { + }).then(function(terms) { return { taxonomies: taxonomies, - terms: response.data + terms: terms }; }); return promise; diff --git a/assets/js/src/newsletter_editor/components/communication.js b/assets/js/src/newsletter_editor/components/communication.js index 439001f525..a3b0ba4c44 100644 --- a/assets/js/src/newsletter_editor/components/communication.js +++ b/assets/js/src/newsletter_editor/components/communication.js @@ -36,36 +36,46 @@ define([ return Module._cachedQuery({ action: 'getTaxonomies', options: { - postType: postType, - }, + postType: postType + } + }).then(function(response) { + return response.data; }); }; Module.getTerms = function(options) { return Module._cachedQuery({ action: 'getTerms', - options: options, + options: options + }).then(function(response) { + return response.data; }); }; Module.getPosts = function(options) { return Module._cachedQuery({ action: 'getPosts', - options: options, + options: options + }).then(function(response) { + return response.data; }); }; Module.getTransformedPosts = function(options) { return Module._cachedQuery({ action: 'getTransformedPosts', - options: options, + options: options + }).then(function(response) { + return response.data; }); }; Module.getBulkTransformedPosts = function(options) { return Module._query({ action: 'getBulkTransformedPosts', - options: options, + options: options + }).then(function(response) { + return response.data; }); }; diff --git a/tests/javascript/newsletter_editor/blocks/automatedLatestContent.spec.js b/tests/javascript/newsletter_editor/blocks/automatedLatestContent.spec.js index 1b65febd1d..30baad156f 100644 --- a/tests/javascript/newsletter_editor/blocks/automatedLatestContent.spec.js +++ b/tests/javascript/newsletter_editor/blocks/automatedLatestContent.spec.js @@ -53,7 +53,7 @@ define([ mock1.expects('trigger').once().withArgs('refreshPosts', postsSet1); mock2.expects('trigger').once().withArgs('refreshPosts', postsSet2); - model.refreshBlocks([block1, block2], { data: [postsSet1, postsSet2] }); + model.refreshBlocks([block1, block2], [postsSet1, postsSet2]); mock1.verify(); mock2.verify(); diff --git a/tests/javascript/newsletter_editor/components/communication.spec.js b/tests/javascript/newsletter_editor/components/communication.spec.js index a2549e7615..97a2a5df12 100644 --- a/tests/javascript/newsletter_editor/components/communication.spec.js +++ b/tests/javascript/newsletter_editor/components/communication.spec.js @@ -73,13 +73,17 @@ define([ expect(spy.args[0][0].data.postType).to.equal('post'); }); - it('fetches post types from the server', function() { + it('fetches taxonomies from the server', function() { var module = CommunicationInjector({ "mailpoet": { Ajax: { post: function() { var deferred = jQuery.Deferred(); - deferred.resolve({ 'category': 'val1' }); + deferred.resolve({ + data: { + 'category': 'val1' + } + }); return deferred; } }, @@ -110,13 +114,13 @@ define([ describe('getTerms', function() { it('sends terms to endpoint', function() { - var spy, - post = function(params) { - var deferred = jQuery.Deferred(); - deferred.resolve({}); - return deferred; - }, - module; + var spy; + var post = function(params) { + var deferred = jQuery.Deferred(); + deferred.resolve({}); + return deferred; + } + var module; spy = sinon.spy(post); module = CommunicationInjector({ "mailpoet": { @@ -138,7 +142,12 @@ define([ Ajax: { post: function() { var deferred = jQuery.Deferred(); - deferred.resolve({ 'term1': 'term1val1', 'term2': 'term2val2' }); + deferred.resolve({ + data: { + 'term1': 'term1val1', + 'term2': 'term2val2' + } + }); return deferred; } }, @@ -201,7 +210,12 @@ define([ Ajax: { post: function() { var deferred = jQuery.Deferred(); - deferred.resolve([{post_title: 'title 1'}, {post_title: 'post title 2'}]); + deferred.resolve({ + data: [ + {post_title: 'title 1'}, + {post_title: 'post title 2'} + ] + }); return deferred; } }, @@ -267,7 +281,12 @@ define([ Ajax: { post: function() { var deferred = jQuery.Deferred(); - deferred.resolve([{type: 'text', text: 'something'}, {type: 'text', text: 'something else'}]); + deferred.resolve({ + data: [ + {type: 'text', text: 'something'}, + {type: 'text', text: 'something else'} + ] + }); return deferred; } },