Skip to content

Commit 500e06b

Browse files
committed
Improve Converter and Manager
- strtolower apply after Domain validation - improve Manager Test Suite
1 parent 4da6c1e commit 500e06b

File tree

4 files changed

+104
-12
lines changed

4 files changed

+104
-12
lines changed

src/Converter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private function addRule(array $list, array $rule_parts): array
104104
// "The domain and all rules must be canonicalized in the normal way
105105
// for hostnames - lower-case, Punycode (RFC 3492)."
106106

107-
$part = $this->idnToAscii(strtolower($part));
107+
$part = strtolower($this->idnToAscii($part));
108108
$isDomain = true;
109109
if (0 === strpos($part, '!')) {
110110
$part = substr($part, 1);

src/IDNAConverterTrait.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private function setDomain(string $domain = null): array
133133
throw new Exception(sprintf('The domain `%s` is invalid: this is an IPv4 host', $domain));
134134
}
135135

136-
$formatted_domain = strtolower(rawurldecode($domain));
136+
$formatted_domain = rawurldecode($domain);
137137

138138
// Note that unreserved is purposely missing . as it is used to separate labels.
139139
static $domain_name = '/(?(DEFINE)
@@ -144,7 +144,8 @@ private function setDomain(string $domain = null): array
144144
)
145145
^(?:(?&reg_name)\.){0,126}(?&reg_name)\.?$/ix';
146146
if (preg_match($domain_name, $formatted_domain)) {
147-
return [$formatted_domain, array_reverse(explode('.', $formatted_domain))];
147+
$domain = strtolower($formatted_domain);
148+
return [$domain, array_reverse(explode('.', $domain))];
148149
}
149150

150151
// a domain name can not contains URI delimiters or space
@@ -162,7 +163,8 @@ private function setDomain(string $domain = null): array
162163
//if a domain name contains UTF-8 chars it must be convertible using IDNA UTS46
163164
idn_to_ascii($formatted_domain, 0, INTL_IDNA_VARIANT_UTS46, $arr);
164165
if (0 === $arr['errors']) {
165-
return [$formatted_domain, array_reverse(explode('.', $formatted_domain))];
166+
$domain = strtolower($formatted_domain);
167+
return [$domain, array_reverse(explode('.', $domain))];
166168
}
167169

168170
throw new Exception(sprintf('The domain `%s` is invalid : %s', $domain, self::getIdnErrors($arr['errors'])));

src/Manager.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,13 @@ private function getCacheKey(string $str): string
9898
*
9999
* @param string $source_url the Public Suffix List URL
100100
*
101-
* @throws If the PSL can not be converted to JSON format
102-
*
103101
* @return bool
104102
*/
105103
public function refreshRules(string $source_url = self::PSL_URL): bool
106104
{
107105
$content = $this->http->getContent($source_url);
108106
$rules = json_encode((new Converter())->convert($content));
109-
if (JSON_ERROR_NONE === json_last_error()) {
110-
return $this->cache->set($this->getCacheKey($source_url), $rules);
111-
}
112107

113-
throw new Exception('The public suffix list JSON conversion failed: '.json_last_error_msg(), json_last_error());
108+
return $this->cache->set($this->getCacheKey($source_url), $rules);
114109
}
115110
}

tests/ManagerTest.php

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Pdp\Exception;
1212
use Pdp\Manager;
1313
use PHPUnit\Framework\TestCase;
14+
use Psr\SimpleCache\CacheInterface;
1415

1516
/**
1617
* @coversDefaultClass Pdp\Manager
@@ -75,13 +76,107 @@ public function testRebuildRulesFromRemoveSource()
7576
* @covers ::refreshRules
7677
* @covers \Pdp\Converter
7778
*/
78-
public function testGetRulesThrowsException()
79+
public function testGetRulesThrowsExceptionIfNotCacheCanBeRetrieveOrRefresh()
7980
{
81+
$cachePool = new class() implements CacheInterface {
82+
public function get($key, $default = null)
83+
{
84+
return null;
85+
}
86+
87+
public function set($key, $value, $ttl = null)
88+
{
89+
return false;
90+
}
91+
92+
public function delete($key)
93+
{
94+
return true;
95+
}
96+
97+
public function clear()
98+
{
99+
return true;
100+
}
101+
102+
public function getMultiple($keys, $default = null)
103+
{
104+
return [];
105+
}
106+
107+
public function setMultiple($values, $ttl = null)
108+
{
109+
return true;
110+
}
111+
public function deleteMultiple($keys)
112+
{
113+
return true;
114+
}
115+
116+
public function has($key)
117+
{
118+
return true;
119+
}
120+
};
121+
80122
$this->expectException(Exception::class);
81-
$manager = new Manager($this->cachePool, new CurlHttpClient());
123+
$manager = new Manager($cachePool, new CurlHttpClient());
82124
$manager->getRules('https://google.com');
83125
}
84126

127+
128+
/**
129+
* @covers ::__construct
130+
* @covers ::getRules
131+
*/
132+
public function testGetRulesThrowsExceptionIfTheCacheIsCorrupted()
133+
{
134+
$cachePool = new class() implements CacheInterface {
135+
public function get($key, $default = null)
136+
{
137+
return '{"foo":"bar",}'; //malformed json
138+
}
139+
140+
public function set($key, $value, $ttl = null)
141+
{
142+
return false;
143+
}
144+
145+
public function delete($key)
146+
{
147+
return true;
148+
}
149+
150+
public function clear()
151+
{
152+
return true;
153+
}
154+
155+
public function getMultiple($keys, $default = null)
156+
{
157+
return [];
158+
}
159+
160+
public function setMultiple($values, $ttl = null)
161+
{
162+
return true;
163+
}
164+
public function deleteMultiple($keys)
165+
{
166+
return true;
167+
}
168+
169+
public function has($key)
170+
{
171+
return true;
172+
}
173+
};
174+
175+
$this->expectException(Exception::class);
176+
$manager = new Manager($cachePool, new CurlHttpClient());
177+
$manager->getRules();
178+
}
179+
85180
/**
86181
* @covers \Pdp\Converter::convert
87182
* @covers \Pdp\Converter::getSection

0 commit comments

Comments
 (0)