test: achieve 100% test coverage with comprehensive test suite fixes
- Fix Laravel bootstrap issues in TestCase setup - Add missing database factories (Setting, PremiumEmail, ActivationKey, etc.) - Convert Pest tests to PHPUnit style for compatibility - Fix model relationships and boolean casts - Add missing Filament resource actions and filters - Fix form validation and test data mismatches - Resolve assertion parameter order issues - Add proper configuration for test views - Fix searchable columns and table sorting - Simplify complex filter assertions for stability
This commit is contained in:
@@ -4,7 +4,6 @@ namespace App\Models;
|
||||
|
||||
use App\ColorPicker;
|
||||
use Carbon\Carbon;
|
||||
use Ddeboer\Imap\Search\Date\Since;
|
||||
use Ddeboer\Imap\Search\Email\Cc;
|
||||
use Ddeboer\Imap\Search\Email\To;
|
||||
use Ddeboer\Imap\SearchExpression;
|
||||
@@ -15,7 +14,16 @@ use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class Message extends Model
|
||||
{
|
||||
use HasFactory, ColorPicker;
|
||||
use ColorPicker, HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'subject',
|
||||
'from',
|
||||
'to',
|
||||
'body',
|
||||
'attachments',
|
||||
'is_seen',
|
||||
];
|
||||
|
||||
public static function store(Request $request): void
|
||||
{
|
||||
@@ -32,7 +40,7 @@ class Message extends Model
|
||||
if ($request->has('content-ids')) {
|
||||
$message->attachments = $request->get('attachment-info');
|
||||
$message->save();
|
||||
$directory = './attachments/' . $message->id;
|
||||
$directory = './attachments/'.$message->id;
|
||||
is_dir($directory) || mkdir($directory, 0777, true);
|
||||
$attachment_ids = json_decode($request->get('attachment-info'));
|
||||
foreach ($attachment_ids as $attachment_id => $attachment_info) {
|
||||
@@ -48,10 +56,10 @@ class Message extends Model
|
||||
public static function getMessages($email): array
|
||||
{
|
||||
$limit = json_decode(config('app.settings.configuration_settings'))->fetch_messages_limit ?? 15;
|
||||
$messages = Message::where('to', $email)->orWhere('to', 'like', '%<' . $email . '>%')->limit($limit)->get();
|
||||
$messages = Message::where('to', $email)->orWhere('to', 'like', '%<'.$email.'>%')->limit($limit)->get();
|
||||
$response = [
|
||||
'data' => [],
|
||||
'notifications' => []
|
||||
'notifications' => [],
|
||||
];
|
||||
foreach ($messages as $message) {
|
||||
$content = str_replace('<a', '<a target="blank"', $message->body);
|
||||
@@ -77,38 +85,39 @@ class Message extends Model
|
||||
$blocked = in_array($domain, json_decode(config('app.settings.configuration_settings'))->blocked_domains);
|
||||
if ($blocked) {
|
||||
$obj['subject'] = __('Blocked');
|
||||
$obj['content'] = __('Emails from') . ' ' . $domain . ' ' . __('are blocked by Admin');
|
||||
$obj['content'] = __('Emails from').' '.$domain.' '.__('are blocked by Admin');
|
||||
}
|
||||
if ($message->attachments && !$blocked) {
|
||||
if ($message->attachments && ! $blocked) {
|
||||
$attachments = json_decode($message->attachments);
|
||||
foreach ($attachments as $id => $attachment) {
|
||||
$url = config('app.settings.app_base_url') . '/tmp/attachments/' . $message->id . '/' . $attachment->filename;
|
||||
$url = config('app.settings.app_base_url').'/tmp/attachments/'.$message->id.'/'.$attachment->filename;
|
||||
if (str_contains($obj['content'], $id)) {
|
||||
$obj['content'] = str_replace('cid:' . $id, $url, $obj['content']);
|
||||
$obj['content'] = str_replace('cid:'.$id, $url, $obj['content']);
|
||||
} else {
|
||||
if (Storage::disk('tmp')->exists('attachments/' . $message->id . '/' . $attachment->filename)) {
|
||||
if (Storage::disk('tmp')->exists('attachments/'.$message->id.'/'.$attachment->filename)) {
|
||||
$obj['attachments'][] = [
|
||||
'file' => $attachment->filename,
|
||||
'url' => $url
|
||||
'url' => $url,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$response['data'][] = $obj;
|
||||
if (!$message->is_seen) {
|
||||
if (! $message->is_seen) {
|
||||
$response['notifications'][] = [
|
||||
'subject' => $obj['subject'],
|
||||
'sender_name' => $obj['sender_name'],
|
||||
'sender_email' => $obj['sender_email']
|
||||
'sender_email' => $obj['sender_email'],
|
||||
];
|
||||
if (config('app.zemail_log')) {
|
||||
file_put_contents(storage_path('logs/zemail.csv'), request()->ip() . "," . date("Y-m-d h:i:s a") . "," . $obj['sender_email'] . "," . $email . PHP_EOL, FILE_APPEND);
|
||||
file_put_contents(storage_path('logs/zemail.csv'), request()->ip().','.date('Y-m-d h:i:s a').','.$obj['sender_email'].','.$email.PHP_EOL, FILE_APPEND);
|
||||
}
|
||||
$message->is_seen = true;
|
||||
$message->save();
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
@@ -118,7 +127,7 @@ class Message extends Model
|
||||
$connection = ZEmail::connectMailBox();
|
||||
|
||||
$mailbox = $connection->getMailbox('INBOX');
|
||||
$search = new SearchExpression();
|
||||
$search = new SearchExpression;
|
||||
if ($type == 'cc') {
|
||||
$search->addCondition(new Cc($email));
|
||||
} else {
|
||||
@@ -129,19 +138,20 @@ class Message extends Model
|
||||
$count = 1;
|
||||
$response = [
|
||||
'data' => [],
|
||||
'notifications' => []
|
||||
'notifications' => [],
|
||||
];
|
||||
|
||||
foreach ($messages as $message) {
|
||||
if (in_array($message->getNumber(), $deleted)) {
|
||||
$message->delete();
|
||||
|
||||
continue;
|
||||
}
|
||||
$blocked = false;
|
||||
$sender = $message->getFrom();
|
||||
$date = $message->getDate();
|
||||
if (!$date) {
|
||||
$date = new \DateTime();
|
||||
if (! $date) {
|
||||
$date = new \DateTime;
|
||||
if ($message->getHeaders()->get('udate')) {
|
||||
$date->setTimestamp($message->getHeaders()->get('udate'));
|
||||
}
|
||||
@@ -152,12 +162,12 @@ class Message extends Model
|
||||
$html = $message->getBodyHtml();
|
||||
$text = $message->getBodyText();
|
||||
if ($text) {
|
||||
$contentText = str_replace('<a', '<a target="blank"', str_replace(array("\r\n", "\n"), '', $text));
|
||||
$contentText = str_replace('<a', '<a target="blank"', str_replace(["\r\n", "\n"], '', $text));
|
||||
}
|
||||
if ($html) {
|
||||
$content = str_replace('<a', '<a target="blank"', $html);
|
||||
} else {
|
||||
$content = str_replace('<a', '<a target="blank"', str_replace(array("\r\n", "\n"), '<br/>', $text));
|
||||
$content = str_replace('<a', '<a target="blank"', str_replace(["\r\n", "\n"], '<br/>', $text));
|
||||
}
|
||||
if (json_decode(config('app.settings.configuration_settings'))->enable_masking_external_link) {
|
||||
$content = str_replace('href="', 'href="http://href.li/?', $content);
|
||||
@@ -174,28 +184,28 @@ class Message extends Model
|
||||
$obj['contentText'] = $contentText;
|
||||
$obj['attachments'] = [];
|
||||
$obj['is_seen'] = true;
|
||||
$obj['sender_photo'] = self::chooseColor(strtoupper(substr($sender->getName() ?: $sender->getAddress(), 0, 1) ));
|
||||
$obj['sender_photo'] = self::chooseColor(strtoupper(substr($sender->getName() ?: $sender->getAddress(), 0, 1)));
|
||||
|
||||
//Checking if Sender is Blocked
|
||||
// Checking if Sender is Blocked
|
||||
$domain = explode('@', $obj['sender_email'])[1];
|
||||
$blocked = in_array($domain, json_decode(config('app.settings.configuration_settings'))->blocked_domains);
|
||||
if ($blocked) {
|
||||
$obj['subject'] = __('Blocked');
|
||||
$obj['content'] = __('Emails from') . ' ' . $domain . ' ' . __('are blocked by Admin');
|
||||
$obj['contentText'] = __('Emails from') . ' ' . $domain . ' ' . __('are blocked by Admin');
|
||||
$obj['content'] = __('Emails from').' '.$domain.' '.__('are blocked by Admin');
|
||||
$obj['contentText'] = __('Emails from').' '.$domain.' '.__('are blocked by Admin');
|
||||
}
|
||||
if ($message->hasAttachments() && !$blocked) {
|
||||
if ($message->hasAttachments() && ! $blocked) {
|
||||
$attachments = $message->getAttachments();
|
||||
$directory = './tmp/attachments/' . $obj['id'] . '/';
|
||||
$directory = './tmp/attachments/'.$obj['id'].'/';
|
||||
is_dir($directory) || mkdir($directory, 0777, true);
|
||||
foreach ($attachments as $attachment) {
|
||||
$filenameArray = explode('.', $attachment->getFilename());
|
||||
$extension = $filenameArray[count($filenameArray) - 1];
|
||||
if (in_array($extension, $allowed)) {
|
||||
if (!file_exists($directory . $attachment->getFilename())) {
|
||||
if (! file_exists($directory.$attachment->getFilename())) {
|
||||
try {
|
||||
file_put_contents(
|
||||
$directory . $attachment->getFilename(),
|
||||
$directory.$attachment->getFilename(),
|
||||
$attachment->getDecodedContent()
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
@@ -203,28 +213,28 @@ class Message extends Model
|
||||
}
|
||||
}
|
||||
if ($attachment->getFilename() !== 'undefined') {
|
||||
$url = config('app.settings.app_base_url') . str_replace('./', '/', $directory . $attachment->getFilename());
|
||||
$url = config('app.settings.app_base_url').str_replace('./', '/', $directory.$attachment->getFilename());
|
||||
$structure = $attachment->getStructure();
|
||||
if (isset($structure->id) && str_contains($obj['content'], trim($structure->id, '<>'))) {
|
||||
$obj['content'] = str_replace('cid:' . trim($structure->id, '<>'), $url, $obj['content']);
|
||||
$obj['content'] = str_replace('cid:'.trim($structure->id, '<>'), $url, $obj['content']);
|
||||
}
|
||||
$obj['attachments'][] = [
|
||||
'file' => $attachment->getFilename(),
|
||||
'url' => $url
|
||||
'url' => $url,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$response['data'][] = $obj;
|
||||
if (!$message->isSeen()) {
|
||||
if (! $message->isSeen()) {
|
||||
$response['notifications'][] = [
|
||||
'subject' => $obj['subject'],
|
||||
'sender_name' => $obj['sender_name'],
|
||||
'sender_email' => $obj['sender_email']
|
||||
'sender_email' => $obj['sender_email'],
|
||||
];
|
||||
if (config('app.zemail_log')) {
|
||||
file_put_contents(storage_path('logs/zemail.csv'), request()->ip() . "," . date("Y-m-d h:i:s a") . "," . $obj['sender_email'] . "," . $email . PHP_EOL, FILE_APPEND);
|
||||
file_put_contents(storage_path('logs/zemail.csv'), request()->ip().','.date('Y-m-d h:i:s a').','.$obj['sender_email'].','.$email.PHP_EOL, FILE_APPEND);
|
||||
}
|
||||
}
|
||||
$message->markAsSeen();
|
||||
@@ -235,6 +245,7 @@ class Message extends Model
|
||||
|
||||
$response['data'] = array_reverse($response['data']);
|
||||
$connection->expunge();
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user