From c1e399ae1e495f8785acc06a135503735490030a Mon Sep 17 00:00:00 2001 From: Paul Scarrone Date: Tue, 20 Jun 2017 23:55:36 -0400 Subject: [PATCH 1/2] Add filter to manager so CSS and JS resources are not mixed when outputing by type --- phalcon/assets/manager.zep | 14 +++++- tests/unit/Assets/ManagerTest.php | 80 +++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/phalcon/assets/manager.zep b/phalcon/assets/manager.zep index 11d936ff327..257897f59ab 100644 --- a/phalcon/assets/manager.zep +++ b/phalcon/assets/manager.zep @@ -287,6 +287,18 @@ class Manager return collection; } + public function collectionResourcesByType(array resources, string type) -> array + { + var $filtered = [], $resource; + for $resource in resources { + if $resource->getType() == type { + let $filtered[] = $resource; + } + } + + return $filtered; + } + /** * Traverses a collection calling the callback to generate its HTML * @@ -310,7 +322,7 @@ class Manager /** * Get the resources as an array */ - let resources = collection->getResources(); + let resources = this->collectionResourcesByType(collection->getResources(), type); /** * Get filters in the collection diff --git a/tests/unit/Assets/ManagerTest.php b/tests/unit/Assets/ManagerTest.php index c6907fb36e8..a1e1a6f613a 100644 --- a/tests/unit/Assets/ManagerTest.php +++ b/tests/unit/Assets/ManagerTest.php @@ -132,6 +132,44 @@ function () { ); } + /** + * Tests addCss and addJs + * + * @author Paul Scarrone + * @since 2017-06-20 + */ + public function testAssetsManagerAddingCssAndJs() + { + $this->specify( + "The combination of addCss and addJs on assets manager does add resources correctly", + function () { + $assets = new Manager(); + + $assets->addCss('/css/style1.css'); + $assets->addCss('/css/style2.css'); + $assets->addJs('/js/script1.js'); + $assets->addJs('/js/script2.js'); + + $collectionCss = $assets->get('css'); + $collectionJs = $assets->get('js'); + + $CSSnumber = 0; + foreach ($collectionCss as $resource) { + expect('css')->equals($resource->getType()); + $CSSnumber++; + } + expect($CSSnumber)->equals(2); + + $JSnumber = 0; + foreach ($collectionJs as $resource) { + expect('js')->equals($resource->getType()); + $JSnumber++; + } + expect($JSnumber)->equals(2); + } + ); + } + /** * Tests addJs * @@ -374,6 +412,48 @@ function () { ); } + /** + * Tests collection with mixed resources + * + * @author Paul Scarrone + * @since 2017-06-20 + */ + public function testAssetsManagerOutputJsWithMixedResourceCollection() + { + $this->specify( + "The outputJs using a mixed resource collection returns only JS resources", + function () { + $assets = new Manager(); + + $assets->collection('header') + ->setPrefix('http:://cdn.example.com/') + ->setLocal(false) + ->addJs('js/script1.js') + ->addJs('js/script2.js') + ->addCss('css/styles1.css') + ->addCss('css/styles2.css'); + $assets->useImplicitOutput(false); + + $actualJS = $assets->outputJs('header'); + $expectedJS = sprintf( + "%s\n%s\n", + '', + '' + ); + expect($actualJS)->equals($expectedJS); + + $actualCSS = $assets->outputCss('header'); + $expectedCSS = sprintf( + "%s\n%s\n", + '', + '' + ); + expect($actualCSS)->equals($expectedCSS); + } + ); + } + + /** * Tests setting local target * From 765d74bc809db9a7f727c50b2f4d39d6ae0ed4ec Mon Sep 17 00:00:00 2001 From: Paul Scarrone Date: Wed, 21 Jun 2017 08:17:32 -0400 Subject: [PATCH 2/2] Update changelog.md with fix for cphalcon/issues#2408 mixed resource collections --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 943a566b129..5fa51faa805 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # [3.2.1](https://github.com/phalcon/cphalcon/releases/tag/v3.2.1) (2017-XX-XX) - Fixed inconsistent behaviour of `Phalcon\Config::merge` across minor version of PHP7 [#12779](https://github.com/phalcon/cphalcon/issues/12779) - Fixed visibility of `Phalcon\Mvc\Model\Query\Builder::{_conditionNotIn,_conditionIn,_conditionNotBetween,_conditionBetween}` to allow 3rd party libraries extend it +- Fixed `Phalcon\Assets\Manager::output`, implemented missing resource type filtering for mixed resource collections [#2408](https://github.com/phalcon/cphalcon/issues/2408) # [3.2.0](https://github.com/phalcon/cphalcon/releases/tag/v3.2.0) (2017-06-19) - Phalcon will now trigger `E_DEPREACATED` by using `Phalcon\Mvc\Model\Criteria::addWhere`, `Phalcon\Debug::getMajorVersion`, `Phalcon\Dispatcher::setModelBinding`, `Phalcon\Tag::resetInput`, `Phalcon\Mvc\Model\Validator::__construct`