From 4331c54c64698acefa92de0841decde144d965d5 Mon Sep 17 00:00:00 2001 From: Andy Carter Date: Thu, 8 Feb 2018 21:49:35 +0000 Subject: [PATCH] Switch to drmonkeyninja/social-share-url library (#17) * Use drmonkeyninja/social-share-url * Update docblock * Update installation steps in readme --- README.md | 4 +- composer.json | 3 +- src/View/Helper/SocialShareHelper.php | 52 +------ .../View/Helper/SocialShareHelperTest.php | 131 ++++-------------- 4 files changed, 38 insertions(+), 152 deletions(-) diff --git a/README.md b/README.md index 742a412..3565f24 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,10 @@ Then add the following line to your bootstrap.php to load the plugin. Plugin::load('SocialShare'); ``` -Also don't forget to add the helper in your controller:- +Also don't forget to add the helper in your `AppView`:- ```php -public $helpers = ['SocialShare.SocialShare']; +$this->loadHelper('SocialShare.SocialShare'); ``` ## Usage diff --git a/composer.json b/composer.json index 4a4599a..919888e 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ } ], "require": { - "cakephp/cakephp": "~3.0" + "cakephp/cakephp": "~3.0", + "drmonkeyninja/social-share-url": "^1.0" }, "autoload": { "psr-4": { diff --git a/src/View/Helper/SocialShareHelper.php b/src/View/Helper/SocialShareHelper.php index 7134a86..53cebbc 100644 --- a/src/View/Helper/SocialShareHelper.php +++ b/src/View/Helper/SocialShareHelper.php @@ -4,6 +4,7 @@ use Cake\Routing\Router; use Cake\View\Helper; +use drmonkeyninja\SocialShareUrl\SocialShareUrl; class SocialShareHelper extends Helper { @@ -20,33 +21,6 @@ class SocialShareHelper extends Helper 'default_fa' => 'fa-share-alt' ]; - /** - * An array of services and their corresponding share/bookmarking URLs. - * - * @var array - */ - protected $_urls = [ - 'delicious' => 'http://delicious.com/post?url={url}&title={text}', - 'digg' => 'http://digg.com/submit?url={url}&title={text}', - 'email' => 'mailto:?subject={text}&body={url}', - 'evernote' => 'http://www.evernote.com/clip.action?url={url}&title={text}', - 'facebook' => 'https://www.facebook.com/sharer/sharer.php?u={url}', - 'friendfeed' => 'http://www.friendfeed.com/share?url={url}&title={text}', - 'google' => 'http://www.google.com/bookmarks/mark?op=edit&bkmk={url}&title={text}', - 'gplus' => 'https://plus.google.com/share?url={url}', - 'linkedin' => 'http://www.linkedin.com/shareArticle?mini=true&url={url}&title={text}', - 'newsvine' => 'http://www.newsvine.com/_tools/seed&save?u={url}&h={text}', - 'pinterest' => 'http://www.pinterest.com/pin/create/button/?url={url}&media={image}&description={text}', - 'pocket' => 'https://getpocket.com/save?url={url}&title={text}', - 'reddit' => 'http://www.reddit.com/submit?url={url}&title={text}', - 'slashdot' => 'http://slashdot.org/bookmark.pl?url={url}&title={text}', - 'stumbleupon' => 'http://www.stumbleupon.com/submit?url={url}&title={text}', - 'technorati' => 'http://technorati.com/faves?add={url}&title={text}', - 'tumblr' => 'http://www.tumblr.com/share?v=3&u={url}&t={text}', - 'twitter' => 'http://twitter.com/home?status={text}+{url}', - 'whatsapp' => 'whatsapp://send?text={text}%20{url}' - ]; - /** * An array mapping services to their Font Awesome icons. * @@ -76,7 +50,7 @@ class SocialShareHelper extends Helper */ public function services() { - return array_keys($this->_urls); + return (new SocialShareUrl())->getServices(); } /** @@ -98,25 +72,9 @@ public function href($service, $url = null, array $options = []) { // Get the URL, get the current full path if a URL hasn't been specified. $url = Router::url($url, true); + $SocialShareUrl = new SocialShareUrl(); - $text = !empty($options['text']) ? $options['text'] : ''; - $image = !empty($options['image']) ? $options['image'] : ''; - - if (!empty($this->_urls[$service])) { - return preg_replace( - [ - '/{url}/', - '/{text}/', - '/{image}/' - ], - [ - urlencode($url), - urlencode($text), - urlencode($image) - ], - $this->_urls[$service] - ); - } + return $SocialShareUrl->getUrl($service, $url, $options); } /** @@ -198,7 +156,7 @@ public function fa($service, $url = null, array $options = []) * * - `icon_class` Class name of icon for overriding defaults. * - * @param string $service Social Media service to create share link for. + * @param string $service Social Media service to create icon for * @param array $options Icon options * @return string */ diff --git a/tests/TestCase/View/Helper/SocialShareHelperTest.php b/tests/TestCase/View/Helper/SocialShareHelperTest.php index 5a3df92..9c8a48c 100644 --- a/tests/TestCase/View/Helper/SocialShareHelperTest.php +++ b/tests/TestCase/View/Helper/SocialShareHelperTest.php @@ -10,76 +10,35 @@ class SocialShareHelperTest extends TestCase { - /** - * @return void - */ - public function setUp() - { - parent::setUp(); - - $View = new View(new Request()); - $this->SocialShare = new SocialShareHelper($View); - } - /** * @return void */ public function testServices() { - $result = $this->SocialShare->services(); + $SocialShare = new SocialShareHelper(new View); + $result = $SocialShare->services(); $this->assertNotEmpty($result); } /** * @return void */ - public function testHref() + public function testIcon() { - $urls = [ - 'delicious' => 'http://delicious.com/post?url=http%3A%2F%2Fexample.com&title=Foo+bar', - 'digg' => 'http://digg.com/submit?url=http%3A%2F%2Fexample.com&title=Foo+bar', - 'email' => 'mailto:?subject=Foo+bar&body=http%3A%2F%2Fexample.com', - 'evernote' => 'http://www.evernote.com/clip.action?url=http%3A%2F%2Fexample.com&title=Foo+bar', - 'facebook' => 'https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com', - 'friendfeed' => 'http://www.friendfeed.com/share?url=http%3A%2F%2Fexample.com&title=Foo+bar', - 'google' => 'http://www.google.com/bookmarks/mark?op=edit&bkmk=http%3A%2F%2Fexample.com&title=Foo+bar', - 'gplus' => 'https://plus.google.com/share?url=http%3A%2F%2Fexample.com', - 'linkedin' => 'http://www.linkedin.com/shareArticle?mini=true&url=http%3A%2F%2Fexample.com&title=Foo+bar', - 'newsvine' => 'http://www.newsvine.com/_tools/seed&save?u=http%3A%2F%2Fexample.com&h=Foo+bar', - 'pinterest' => 'http://www.pinterest.com/pin/create/button/?url=http%3A%2F%2Fexample.com&media=http%3A%2F%2Fexample.com%2Ftest.jpg&description=Foo+bar', - 'pocket' => 'https://getpocket.com/save?url=http%3A%2F%2Fexample.com&title=Foo+bar', - 'reddit' => 'http://www.reddit.com/submit?url=http%3A%2F%2Fexample.com&title=Foo+bar', - 'slashdot' => 'http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fexample.com&title=Foo+bar', - 'stumbleupon' => 'http://www.stumbleupon.com/submit?url=http%3A%2F%2Fexample.com&title=Foo+bar', - 'technorati' => 'http://technorati.com/faves?add=http%3A%2F%2Fexample.com&title=Foo+bar', - 'tumblr' => 'http://www.tumblr.com/share?v=3&u=http%3A%2F%2Fexample.com&t=Foo+bar', - 'twitter' => 'http://twitter.com/home?status=Foo+bar+http%3A%2F%2Fexample.com', - 'whatsapp' => 'whatsapp://send?text=Foo+bar%20http%3A%2F%2Fexample.com' - ]; - - $options = [ - 'text' => 'Foo bar', - 'image' => 'http://example.com/test.jpg' - ]; - - foreach ($urls as $service => $expected) { - $this->assertEquals( - $expected, - $this->SocialShare->href($service, 'http://example.com', $options) - ); - } + $SocialShare = new SocialShareHelper(new View); + $icon = $SocialShare->icon('facebook'); + $expected = ''; + $this->assertSame($expected, $icon); } /** * @return void */ - public function testIcon() { - $icon = $this->SocialShare->icon('facebook'); - $expected = ''; - $this->assertSame($expected, $icon); - + public function testIconClassCanBeOverridden() + { + $SocialShare = new SocialShareHelper(new View); $expected = ''; - $icon = $this->SocialShare->icon('whatsapp', ['icon_class' => 'fa fa-whatsapp-square']); + $icon = $SocialShare->icon('whatsapp', ['icon_class' => 'fa fa-whatsapp-square']); $this->assertSame($expected, $icon); } @@ -88,40 +47,23 @@ public function testIcon() { */ public function testLinks() { - // Facebook test + $SocialShare = $this->getMockBuilder(SocialShareHelper::class) + ->setMethods(['href']) + ->setConstructorArgs([new View()]) + ->getMock(); + $SocialShare->expects($this->once()) + ->method('href') + ->willReturn('https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com'); + $expected = 'Share'; $this->assertEquals( $expected, - $this->SocialShare->link( + $SocialShare->link( 'facebook', 'Share', 'http://example.com' ) ); - - // Twitter test - $expected = 'Share'; - $this->assertEquals( - $expected, - $this->SocialShare->link( - 'twitter', - 'Share', - 'http://example.com', - ['text' => 'Foo bar'] - ) - ); - - // Facebook test - $expected = 'Share'; - $this->assertEquals( - $expected, - $this->SocialShare->link( - 'facebook', - 'Share', - 'http://example.com', - ['target' => null] - ) - ); } /** @@ -129,36 +71,21 @@ public function testLinks() */ public function testFa() { - // Font Awesome test + $SocialShare = $this->getMockBuilder(SocialShareHelper::class) + ->setMethods(['href']) + ->setConstructorArgs([new View()]) + ->getMock(); + $SocialShare->expects($this->once()) + ->method('href') + ->willReturn('https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com'); + $expected = ''; $this->assertEquals( $expected, - $this->SocialShare->fa( + $SocialShare->fa( 'facebook', 'http://example.com' ) ); - - // Font Awesome icon class test - $expected = ''; - $this->assertEquals( - $expected, - $this->SocialShare->fa( - 'facebook', - 'http://example.com', - ['icon_class' => 'fa fa-facebook-square'] - ) - ); - - // Custom Text test - $expected = ''; - $this->assertEquals( - $expected, - $this->SocialShare->fa( - 'whatsapp', - 'http://example.com', - ['text' => 'Demo Text test'] - ) - ); } }