Skip to content

Commit

Permalink
param option (#20)
Browse files Browse the repository at this point in the history
* param option

* pr ref

* phpdocs
  • Loading branch information
nadar authored Jan 6, 2021
1 parent 9abe190 commit b1a3411
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 41 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).
In order to read more about upgrading and BC breaks have a look at the [UPGRADE Document](UPGRADE.md).

## 1.1.0 (6. January 2020)

+ [#20](https://github.com/luyadev/luya-privacy/pull/20) Added option to set custom get param and using the LUYA Url helper appendQuery() method.

## 1.0.6 (28. June 2020)

Expand Down
57 changes: 26 additions & 31 deletions src/widgets/PrivacyWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use luya\helpers\Html;
use luya\privacy\Module;
use luya\privacy\traits\PrivacyTrait;
use luya\helpers\StringHelper;
use luya\helpers\Url;

/**
* Privacy Widget
Expand Down Expand Up @@ -39,6 +39,12 @@ class PrivacyWidget extends Widget
{
use PrivacyTrait;

/**
* @var string The get param which should be used to generate the accept url.
* @since 1.1.0
*/
public $acceptParam = 'acceptCookies';

/**
* @var string|array Provide a string which is the content from the div, or provide an array in order to build a full
* html element.
Expand All @@ -50,7 +56,6 @@ class PrivacyWidget extends Widget
*/
public $acceptButton = [
'tag' => 'a',
'href' => 'acceptCookies=1',
'class' => 'btn btn-primary',
];

Expand Down Expand Up @@ -102,6 +107,17 @@ class PrivacyWidget extends Widget
*/
public $wrapper;

/**
* Builds the accept url with the accept param and the current url and absolute schema
*
* @return string
* @since 1.1.0
*/
public function buildAcceptUrl()
{
return Url::appendQuery([$this->acceptParam => 1], true);
}

/**
* Build the html tag.
*
Expand Down Expand Up @@ -143,15 +159,20 @@ protected function buildTag($config, $defaultTag, $defaultContent)
public function run()
{
// see if user clicks on a button
$acceptCookies = Yii::$app->request->get('acceptCookies');
$acceptCookies = Yii::$app->request->get($this->acceptParam);

// cookie param provided with status accepted
if ($acceptCookies == '1') {
$this->setPrivacyCookieValue(true);
}

if (!array_key_exists('href', $this->acceptButton)) {
$this->acceptButton['href'] = $this->buildAcceptUrl();
}

$this->acceptButton['href'] = $this->createAppendUrl($this->acceptButton['href']);
$this->acceptButton['rel'] = 'nofollow';
if (!array_key_exists('rel', $this->acceptButton)) {
$this->acceptButton['rel'] = 'nofollow';
}

if ($this->declineButton !== false) {
$this->declineButton['rel'] = 'nofollow';
Expand All @@ -173,30 +194,4 @@ public function run()
return $widget;
}
}

/**
*
* Append the accept string to a given url.
*
* @param string $append The string to append to the current url `foo=bar`
* @return string
* @since 1.0.2
*/
public function createAppendUrl($append)
{
$url = Yii::$app->request->url;
$append = ltrim(ltrim($append, '&'), '?');

// use &
if (StringHelper::contains('?', $url)) {
if (StringHelper::endsWith($url, '&')) {
return $url . $append;
}

return $url . '&' . $append;
}

// use ?
return $url . '?' . $append;
}
}
29 changes: 19 additions & 10 deletions tests/widgets/PrivacyWidgetTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
<?php
namespace luya\privacy\tests\widgets;

use luya\helpers\Url;
use Yii;
use luya\privacy\tests\PrivacyTestCase;
use luya\privacy\widgets\PrivacyWidget;

class PrivacyWidgetTest extends PrivacyTestCase
{
public function testChangedGetParam()
{
$w = new PrivacyWidget([
'acceptParam' => 'foo',
]);

$this->assertSame('/?foo=1', $w->buildAcceptUrl());
}
public function testWidgetStandardOutput()
{
$this->assertSameTrimmed('<div class="luya-privacy-widget-container"><div>We use cookies to improve your experience on our website. Please read and accept our privacy policies.</div><a class="btn btn-primary" href="/?acceptCookies=1" rel="nofollow">Accept</a></div>', PrivacyWidget::widget(['forceOutput' => true]));
Expand All @@ -21,27 +30,27 @@ public function testAppendUrl()
{
$w = new PrivacyWidget();

$this->assertSame('/?bar=foo', $w->createAppendUrl('?bar=foo'));
$this->assertSame('/?bar=foo', Url::appendQuery('?bar=foo'));

Yii::$app->request->url = 'https://luya.io?hello=word';
$this->assertSame('https://luya.io?hello=word&bar=foo', $w->createAppendUrl('?bar=foo'));
$this->assertSame('https://luya.io?hello=word&bar=foo', $w->createAppendUrl('bar=foo'));
$this->assertSame('https://luya.io?hello=word&bar=foo', $w->createAppendUrl('&bar=foo'));
$this->assertSame('https://luya.io?hello=word&bar=foo', Url::appendQuery('?bar=foo'));
$this->assertSame('https://luya.io?hello=word&bar=foo', Url::appendQuery('bar=foo'));
$this->assertSame('https://luya.io?hello=word&bar=foo', Url::appendQuery('&bar=foo'));
}

public function testSpecialAppendCase()
{
$w = new PrivacyWidget();
Yii::$app->request->url = 'https://luya.io?hello=word&';
$this->assertSame('https://luya.io?hello=word&bar=foo', $w->createAppendUrl('?bar=foo'));
$this->assertSame('https://luya.io?hello=word&bar=foo', $w->createAppendUrl('bar=foo'));
$this->assertSame('https://luya.io?hello=word&bar=foo', $w->createAppendUrl('&bar=foo'));
$this->assertSame('https://luya.io?hello=word&bar=foo', Url::appendQuery('?bar=foo'));
$this->assertSame('https://luya.io?hello=word&bar=foo', Url::appendQuery('bar=foo'));
$this->assertSame('https://luya.io?hello=word&bar=foo', Url::appendQuery('&bar=foo'));

$w = new PrivacyWidget();
Yii::$app->request->url = 'https://luya.io';
$this->assertSame('https://luya.io?bar=foo', $w->createAppendUrl('?bar=foo'));
$this->assertSame('https://luya.io?bar=foo', $w->createAppendUrl('bar=foo'));
$this->assertSame('https://luya.io?bar=foo', $w->createAppendUrl('&bar=foo'));
$this->assertSame('https://luya.io?bar=foo', Url::appendQuery('?bar=foo'));
$this->assertSame('https://luya.io?bar=foo', Url::appendQuery('bar=foo'));
$this->assertSame('https://luya.io?bar=foo', Url::appendQuery('&bar=foo'));
}

public function testPrivacyState()
Expand Down

0 comments on commit b1a3411

Please sign in to comment.