Skip to content

Commit

Permalink
Switch to drmonkeyninja/social-share-url library (#17)
Browse files Browse the repository at this point in the history
* Use drmonkeyninja/social-share-url

* Update docblock

* Update installation steps in readme
  • Loading branch information
drmonkeyninja authored Feb 8, 2018
1 parent d82f539 commit 4331c54
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 152 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
}
],
"require": {
"cakephp/cakephp": "~3.0"
"cakephp/cakephp": "~3.0",
"drmonkeyninja/social-share-url": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
52 changes: 5 additions & 47 deletions src/View/Helper/SocialShareHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Cake\Routing\Router;
use Cake\View\Helper;
use drmonkeyninja\SocialShareUrl\SocialShareUrl;

class SocialShareHelper extends Helper
{
Expand All @@ -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.
*
Expand Down Expand Up @@ -76,7 +50,7 @@ class SocialShareHelper extends Helper
*/
public function services()
{
return array_keys($this->_urls);
return (new SocialShareUrl())->getServices();
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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
*/
Expand Down
131 changes: 29 additions & 102 deletions tests/TestCase/View/Helper/SocialShareHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<i class="fa fa-facebook"></i>';
$this->assertSame($expected, $icon);
}

/**
* @return void
*/
public function testIcon() {
$icon = $this->SocialShare->icon('facebook');
$expected = '<i class="fa fa-facebook"></i>';
$this->assertSame($expected, $icon);

public function testIconClassCanBeOverridden()
{
$SocialShare = new SocialShareHelper(new View);
$expected = '<i class="fa fa-whatsapp-square"></i>';
$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);
}

Expand All @@ -88,77 +47,45 @@ 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 = '<a href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com" target="_blank">Share</a>';
$this->assertEquals(
$expected,
$this->SocialShare->link(
$SocialShare->link(
'facebook',
'Share',
'http://example.com'
)
);

// Twitter test
$expected = '<a href="http://twitter.com/home?status=Foo+bar+http%3A%2F%2Fexample.com" target="_blank">Share</a>';
$this->assertEquals(
$expected,
$this->SocialShare->link(
'twitter',
'Share',
'http://example.com',
['text' => 'Foo bar']
)
);

// Facebook test
$expected = '<a href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com">Share</a>';
$this->assertEquals(
$expected,
$this->SocialShare->link(
'facebook',
'Share',
'http://example.com',
['target' => null]
)
);
}

/**
* @return void
*/
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 = '<a href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com" target="_blank"><i class="fa fa-facebook"></i></a>';
$this->assertEquals(
$expected,
$this->SocialShare->fa(
$SocialShare->fa(
'facebook',
'http://example.com'
)
);

// Font Awesome icon class test
$expected = '<a href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com" target="_blank"><i class="fa fa-facebook-square"></i></a>';
$this->assertEquals(
$expected,
$this->SocialShare->fa(
'facebook',
'http://example.com',
['icon_class' => 'fa fa-facebook-square']
)
);

// Custom Text test
$expected = '<a href="whatsapp://send?text=Demo+Text+test%20http%3A%2F%2Fexample.com" target="_blank"><i class="fa fa-whatsapp"></i></a>';
$this->assertEquals(
$expected,
$this->SocialShare->fa(
'whatsapp',
'http://example.com',
['text' => 'Demo Text test']
)
);
}
}

0 comments on commit 4331c54

Please sign in to comment.