| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace Tools\Test\TestCase\View\Helper;
- use Cake\View\View;
- use Shim\TestSuite\TestCase;
- use Tools\View\Helper\GravatarHelper;
- /**
- * Gravatar Test Case
- */
- class GravatarHelperTest extends TestCase {
- /**
- * @var \Tools\View\Helper\GravatarHelper
- */
- protected $Gravatar;
- /**
- * @var string
- */
- protected $testEmail;
- /**
- * @var string
- */
- protected $garageEmail;
- /**
- * SetUp method
- *
- * @return void
- */
- public function setUp(): void {
- parent::setUp();
- $this->testEmail = 'graham@grahamweldon.com'; // For testing normal behavior
- $this->garageEmail = 'test@test.de'; // For testing default image behavior
- $this->Gravatar = new GravatarHelper(new View(null));
- }
- /**
- * TearDown method
- *
- * @return void
- */
- public function tearDown(): void {
- parent::tearDown();
- unset($this->Gravatar);
- }
- /**
- * @return void
- */
- public function testDefaultImages() {
- $is = $this->Gravatar->defaultImages();
- $expectedCount = 7;
- foreach ($is as $image) {
- //$this->debug($image . ' ');
- }
- $this->assertTrue(is_array($is) && (count($is) === $expectedCount));
- }
- /**
- * @return void
- */
- public function testImage() {
- $is = $this->Gravatar->image($this->garageEmail);
- $this->assertTrue(!empty($is));
- $is = $this->Gravatar->image($this->testEmail);
- $this->assertTextContains('.gravatar.com/avatar/', $is);
- $is = $this->Gravatar->image($this->testEmail, ['size' => '200']);
- $this->assertTextContains('?size=200"', $is);
- $is = $this->Gravatar->image($this->testEmail, ['size' => '20']);
- $this->assertTextContains('?size=20"', $is);
- $is = $this->Gravatar->image($this->testEmail, ['rating' => 'X']); # note the capit. x
- $this->assertTextContains('?rating=x"', $is);
- $is = $this->Gravatar->image($this->testEmail, ['ext' => true]);
- $this->assertTextContains('.jpg"', $is);
- $is = $this->Gravatar->image($this->testEmail, ['default' => 'none']);
- $this->assertTrue(!empty($is));
- $is = $this->Gravatar->image($this->garageEmail, ['default' => 'none']);
- $this->assertTrue(!empty($is));
- $is = $this->Gravatar->image($this->garageEmail, ['default' => 'http://2.gravatar.com/avatar/8379aabc84ecee06f48d8ca48e09eef4?d=identicon']);
- $this->assertTrue(!empty($is));
- $is = $this->Gravatar->image($this->testEmail, ['size' => '20']);
- $this->assertTextContains('?size=20"', $is);
- $is = $this->Gravatar->image($this->testEmail, ['rating' => 'X', 'size' => 20, 'default' => 'none']);
- $this->assertTextContains('?rating=x&size=20&default=none"', $is);
- }
- /**
- * TestBaseUrlGeneration
- *
- * @return void
- */
- public function testBaseUrlGeneration() {
- $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
- $result = $this->Gravatar->url('example@gravatar.com', ['ext' => false, 'default' => 'wavatar']);
- [$url, $params] = explode('?', $result);
- $this->assertEquals($expected, $url);
- }
- /**
- * TestExtensions
- *
- * @return void
- */
- public function testExtensions() {
- $result = $this->Gravatar->url('example@gravatar.com', ['ext' => true, 'default' => 'wavatar']);
- $this->assertMatchesRegularExpression('/\.jpg(?:$|\?)/', $result);
- }
- /**
- * TestRating
- *
- * @return void
- */
- public function testRating() {
- $result = $this->Gravatar->url('example@gravatar.com', ['ext' => true, 'default' => 'wavatar']);
- $this->assertMatchesRegularExpression('/\.jpg(?:$|\?)/', $result);
- }
- /**
- * TestAlternateDefaultIcon
- *
- * @return void
- */
- public function testAlternateDefaultIcon() {
- $result = $this->Gravatar->url('example@gravatar.com', ['ext' => false, 'default' => 'wavatar']);
- [$url, $params] = explode('?', $result);
- $this->assertMatchesRegularExpression('/default=wavatar/', $params);
- }
- /**
- * TestAlternateDefaultIconCorrection
- *
- * @return void
- */
- public function testAlternateDefaultIconCorrection() {
- $result = $this->Gravatar->url('example@gravatar.com', ['ext' => false, 'default' => '12345']);
- $this->assertMatchesRegularExpression('/[^\?]+/', $result);
- }
- /**
- * TestSize
- *
- * @return void
- */
- public function testSize() {
- $result = $this->Gravatar->url('example@gravatar.com', ['size' => '120']);
- [$url, $params] = explode('?', $result);
- $this->assertMatchesRegularExpression('/size=120/', $params);
- }
- /**
- * TestImageTag
- *
- * @return void
- */
- public function testImageTag() {
- $expected = '<img src="http://www.gravatar.com/avatar/' . md5('example@gravatar.com') . '" alt="">';
- $result = $this->Gravatar->image('example@gravatar.com', ['ext' => false]);
- $this->assertEquals($expected, $result);
- $expected = '<img src="http://www.gravatar.com/avatar/' . md5('example@gravatar.com') . '" alt="Gravatar">';
- $result = $this->Gravatar->image('example@gravatar.com', ['ext' => false, 'alt' => 'Gravatar']);
- $this->assertEquals($expected, $result);
- }
- /**
- * TestDefaulting
- *
- * @return void
- */
- public function testDefaulting() {
- $result = $this->Gravatar->url('example@gravatar.com', ['default' => 'wavatar', 'size' => 'default']);
- [$url, $params] = explode('?', $result);
- $this->assertEquals($params, 'default=wavatar');
- }
- /**
- * TestNonSecureUrl
- *
- * @return void
- */
- public function testNonSecureUrl() {
- $_SERVER['HTTPS'] = false;
- $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
- $result = $this->Gravatar->url('example@gravatar.com', ['ext' => false]);
- $this->assertEquals($expected, $result);
- $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
- $result = $this->Gravatar->url('example@gravatar.com', ['ext' => false, 'secure' => false]);
- $this->assertEquals($expected, $result);
- $_SERVER['HTTPS'] = true;
- $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
- $result = $this->Gravatar->url('example@gravatar.com', ['ext' => false, 'secure' => false]);
- $this->assertEquals($expected, $result);
- }
- /**
- * TestSecureUrl
- *
- * @return void
- */
- public function testSecureUrl() {
- $expected = 'https://secure.gravatar.com/avatar/' . md5('example@gravatar.com');
- $result = $this->Gravatar->url('example@gravatar.com', ['ext' => false, 'secure' => true]);
- $this->assertEquals($expected, $result);
- $_SERVER['HTTPS'] = true;
- $this->Gravatar = new GravatarHelper(new View(null));
- $expected = 'https://secure.gravatar.com/avatar/' . md5('example@gravatar.com');
- $result = $this->Gravatar->url('example@gravatar.com', ['ext' => false]);
- $this->assertEquals($expected, $result);
- $expected = 'https://secure.gravatar.com/avatar/' . md5('example@gravatar.com');
- $result = $this->Gravatar->url('example@gravatar.com', ['ext' => false, 'secure' => true]);
- $this->assertEquals($expected, $result);
- }
- }
|