Find this useful? Enter your email to receive occasional updates for securing PHP code.
Signing you up...
Thank you for signing up!
PHP Decode
<?php declare(strict_types=1); /** * This file is part of CodeIgniter 4 framework. * ..
Decoded Output download
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Email;
use CodeIgniter\Events\Events;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockEmail;
use ErrorException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
/**
* @internal
*/
#[Group('Others')]
final class EmailTest extends CIUnitTestCase
{
public function testEmailValidation(): void
{
$config = config('Email');
$config->validate = true;
$email = new Email($config);
$email->setTo('invalid');
$this->assertStringContainsString('Invalid email address: "invalid"', $email->printDebugger());
}
public static function provideEmailSendWithClearance(): iterable
{
return [
'autoclear' => [true],
'not autoclear' => [false],
];
}
/**
* @param mixed $autoClear
*/
#[DataProvider('provideEmailSendWithClearance')]
public function testEmailSendWithClearance($autoClear): void
{
$email = $this->createMockEmail();
$email->setTo('[email protected]');
$this->assertTrue($email->send($autoClear));
if (! $autoClear) {
$this->assertSame('[email protected]', $email->archive['recipients'][0]);
}
}
public function testEmailSendStoresArchive(): void
{
$email = $this->createMockEmail();
$email->setTo('[email protected]');
$email->setFrom('[email protected]');
$email->setSubject('Archive Test');
$this->assertTrue($email->send());
$this->assertNotEmpty($email->archive);
$this->assertSame(['[email protected]'], $email->archive['recipients']);
$this->assertSame('[email protected]', $email->archive['fromEmail']);
$this->assertSame('Archive Test', $email->archive['subject']);
}
public function testAutoClearLeavesArchive(): void
{
$email = $this->createMockEmail();
$email->setTo('[email protected]');
$this->assertTrue($email->send(true));
$this->assertNotEmpty($email->archive);
}
public function testEmailSendRepeatUpdatesArchive(): void
{
$config = config('Email');
$email = new MockEmail($config);
$email->setTo('[email protected]');
$email->setFrom('[email protected]');
$this->assertTrue($email->send());
$email->setFrom('');
$email->setSubject('Archive Test');
$this->assertTrue($email->send());
$this->assertSame('', $email->archive['fromEmail']);
$this->assertSame('Archive Test', $email->archive['subject']);
}
public function testSuccessDoesTriggerEvent(): void
{
$email = $this->createMockEmail();
$email->setTo('[email protected]');
$result = null;
Events::on('email', static function ($arg) use (&$result): void {
$result = $arg;
});
$this->assertTrue($email->send());
$this->assertIsArray($result);
$this->assertSame(['[email protected]'], $result['recipients']);
}
public function testFailureDoesNotTriggerEvent(): void
{
$email = $this->createMockEmail();
$email->setTo('[email protected]');
$email->returnValue = false;
$result = null;
Events::on('email', static function ($arg) use (&$result): void {
$result = $arg;
});
$this->assertFalse($email->send());
$this->assertNull($result);
}
public function testDestructDoesNotThrowException(): void
{
$email = $this->getMockBuilder(Email::class)
->disableOriginalConstructor()
->onlyMethods(['sendCommand'])
->getMock();
$email->expects($this->once())->method('sendCommand')
->willThrowException(new ErrorException('SMTP Error.'));
// Force resource to be injected into the property
$SMTPConnect = fopen(__FILE__, 'rb');
$this->setPrivateProperty($email, 'SMTPConnect', $SMTPConnect);
$email->__destruct();
}
private function createMockEmail(): MockEmail
{
$config = config('Email');
$config->validate = true;
return new MockEmail($config);
}
public function testSetAttachmentCIDFile(): void
{
$email = $this->createMockEmail();
$email->setFrom('[email protected]', 'Your Name');
$email->setTo('[email protected]');
$filename = SUPPORTPATH . 'Images/ci-logo.png';
$email->attach($filename);
$cid = $email->setAttachmentCID($filename);
$email->setMessage('<img src="cid:' . $cid . '" alt="CI Logo">');
$this->assertTrue($email->send());
$this->assertStringStartsWith('ci-logo.png@', $cid);
$this->assertStringStartsWith(
'ci-logo.png@',
$email->archive['attachments'][0]['cid']
);
$this->assertMatchesRegularExpression(
'/<img src="cid:ci-logo.png@(.+?)" alt="CI Logo">/u',
$email->archive['body']
);
}
public function testSetAttachmentCIDBufferString(): void
{
$email = $this->createMockEmail();
$email->setFrom('[email protected]', 'Your Name');
$email->setTo('[email protected]');
$filename = SUPPORTPATH . 'Images/ci-logo.png';
$imageData = file_get_contents($filename);
$email->attach($imageData, 'inline', 'image001.png', 'image/png');
$cid = $email->setAttachmentCID('image001.png');
$email->setMessage('<img src="cid:' . $cid . '" alt="CI Logo">');
$this->assertTrue($email->send());
$this->assertStringStartsWith('image001.png@', $cid);
$this->assertStringStartsWith(
'image001.png@',
$email->archive['attachments'][0]['cid']
);
$this->assertMatchesRegularExpression(
'/<img src="cid:image001.png@(.+?)" alt="CI Logo">/u',
$email->archive['body']
);
}
}
?>
Did this file decode correctly?
Original Code
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Email;
use CodeIgniter\Events\Events;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockEmail;
use ErrorException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
/**
* @internal
*/
#[Group('Others')]
final class EmailTest extends CIUnitTestCase
{
public function testEmailValidation(): void
{
$config = config('Email');
$config->validate = true;
$email = new Email($config);
$email->setTo('invalid');
$this->assertStringContainsString('Invalid email address: "invalid"', $email->printDebugger());
}
public static function provideEmailSendWithClearance(): iterable
{
return [
'autoclear' => [true],
'not autoclear' => [false],
];
}
/**
* @param mixed $autoClear
*/
#[DataProvider('provideEmailSendWithClearance')]
public function testEmailSendWithClearance($autoClear): void
{
$email = $this->createMockEmail();
$email->setTo('[email protected]');
$this->assertTrue($email->send($autoClear));
if (! $autoClear) {
$this->assertSame('[email protected]', $email->archive['recipients'][0]);
}
}
public function testEmailSendStoresArchive(): void
{
$email = $this->createMockEmail();
$email->setTo('[email protected]');
$email->setFrom('[email protected]');
$email->setSubject('Archive Test');
$this->assertTrue($email->send());
$this->assertNotEmpty($email->archive);
$this->assertSame(['[email protected]'], $email->archive['recipients']);
$this->assertSame('[email protected]', $email->archive['fromEmail']);
$this->assertSame('Archive Test', $email->archive['subject']);
}
public function testAutoClearLeavesArchive(): void
{
$email = $this->createMockEmail();
$email->setTo('[email protected]');
$this->assertTrue($email->send(true));
$this->assertNotEmpty($email->archive);
}
public function testEmailSendRepeatUpdatesArchive(): void
{
$config = config('Email');
$email = new MockEmail($config);
$email->setTo('[email protected]');
$email->setFrom('[email protected]');
$this->assertTrue($email->send());
$email->setFrom('');
$email->setSubject('Archive Test');
$this->assertTrue($email->send());
$this->assertSame('', $email->archive['fromEmail']);
$this->assertSame('Archive Test', $email->archive['subject']);
}
public function testSuccessDoesTriggerEvent(): void
{
$email = $this->createMockEmail();
$email->setTo('[email protected]');
$result = null;
Events::on('email', static function ($arg) use (&$result): void {
$result = $arg;
});
$this->assertTrue($email->send());
$this->assertIsArray($result);
$this->assertSame(['[email protected]'], $result['recipients']);
}
public function testFailureDoesNotTriggerEvent(): void
{
$email = $this->createMockEmail();
$email->setTo('[email protected]');
$email->returnValue = false;
$result = null;
Events::on('email', static function ($arg) use (&$result): void {
$result = $arg;
});
$this->assertFalse($email->send());
$this->assertNull($result);
}
public function testDestructDoesNotThrowException(): void
{
$email = $this->getMockBuilder(Email::class)
->disableOriginalConstructor()
->onlyMethods(['sendCommand'])
->getMock();
$email->expects($this->once())->method('sendCommand')
->willThrowException(new ErrorException('SMTP Error.'));
// Force resource to be injected into the property
$SMTPConnect = fopen(__FILE__, 'rb');
$this->setPrivateProperty($email, 'SMTPConnect', $SMTPConnect);
$email->__destruct();
}
private function createMockEmail(): MockEmail
{
$config = config('Email');
$config->validate = true;
return new MockEmail($config);
}
public function testSetAttachmentCIDFile(): void
{
$email = $this->createMockEmail();
$email->setFrom('[email protected]', 'Your Name');
$email->setTo('[email protected]');
$filename = SUPPORTPATH . 'Images/ci-logo.png';
$email->attach($filename);
$cid = $email->setAttachmentCID($filename);
$email->setMessage('<img src="cid:' . $cid . '" alt="CI Logo">');
$this->assertTrue($email->send());
$this->assertStringStartsWith('ci-logo.png@', $cid);
$this->assertStringStartsWith(
'ci-logo.png@',
$email->archive['attachments'][0]['cid']
);
$this->assertMatchesRegularExpression(
'/<img src="cid:ci-logo.png@(.+?)" alt="CI Logo">/u',
$email->archive['body']
);
}
public function testSetAttachmentCIDBufferString(): void
{
$email = $this->createMockEmail();
$email->setFrom('[email protected]', 'Your Name');
$email->setTo('[email protected]');
$filename = SUPPORTPATH . 'Images/ci-logo.png';
$imageData = file_get_contents($filename);
$email->attach($imageData, 'inline', 'image001.png', 'image/png');
$cid = $email->setAttachmentCID('image001.png');
$email->setMessage('<img src="cid:' . $cid . '" alt="CI Logo">');
$this->assertTrue($email->send());
$this->assertStringStartsWith('image001.png@', $cid);
$this->assertStringStartsWith(
'image001.png@',
$email->archive['attachments'][0]['cid']
);
$this->assertMatchesRegularExpression(
'/<img src="cid:image001.png@(.+?)" alt="CI Logo">/u',
$email->archive['body']
);
}
}
Function Calls
None |
Stats
MD5 | c17dab3a61e6c8197a77be7e5eaa0668 |
Eval Count | 0 |
Decode Time | 113 ms |