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:
@@ -2,14 +2,11 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
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;
|
||||
use Ddeboer\Imap\Server;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Cookie;
|
||||
|
||||
use function str_replace;
|
||||
|
||||
class ZEmail extends Model
|
||||
@@ -19,13 +16,14 @@ class ZEmail extends Model
|
||||
if ($imap === null) {
|
||||
$imap = json_decode(config('app.settings.imap_settings'), true);
|
||||
}
|
||||
$flags = $imap['protocol'] . '/' . $imap['encryption'];
|
||||
$flags = $imap['protocol'].'/'.$imap['encryption'];
|
||||
if ($imap['validate_cert']) {
|
||||
$flags = $flags . '/validate-cert';
|
||||
$flags = $flags.'/validate-cert';
|
||||
} else {
|
||||
$flags = $flags . '/novalidate-cert';
|
||||
$flags = $flags.'/novalidate-cert';
|
||||
}
|
||||
$server = new Server($imap['host'], $imap['port'], $flags);
|
||||
|
||||
return $server->authenticate($imap['username'], $imap['password']);
|
||||
}
|
||||
|
||||
@@ -44,6 +42,7 @@ class ZEmail extends Model
|
||||
return Message::fetchMessages($email, $type, $deleted);
|
||||
}
|
||||
}
|
||||
|
||||
return Message::fetchMessages($email, $type, $deleted);
|
||||
}
|
||||
|
||||
@@ -55,7 +54,8 @@ class ZEmail extends Model
|
||||
$connection->expunge();
|
||||
}
|
||||
|
||||
public static function getEmail($generate = false) {
|
||||
public static function getEmail($generate = false)
|
||||
{
|
||||
if (Cookie::has('email')) {
|
||||
return Cookie::get('email');
|
||||
} else {
|
||||
@@ -63,7 +63,8 @@ class ZEmail extends Model
|
||||
}
|
||||
}
|
||||
|
||||
public static function getEmails() {
|
||||
public static function getEmails()
|
||||
{
|
||||
if (Cookie::has('emails')) {
|
||||
return unserialize(Cookie::get('emails'));
|
||||
} else {
|
||||
@@ -100,10 +101,11 @@ class ZEmail extends Model
|
||||
$data = explode('@', $email);
|
||||
$username = $data[0];
|
||||
if (strlen($username) < json_decode(config('app.settings.configuration_settings'))->custom_username_length_min || strlen($username) > json_decode(config('app.settings.configuration_settings'))->custom_username_length_max) {
|
||||
$zemail = new ZEmail();
|
||||
$zemail = new ZEmail;
|
||||
$username = $zemail->generateRandomUsername();
|
||||
}
|
||||
$domain = $data[1];
|
||||
|
||||
return ZEmail::createCustomEmail($username, $domain);
|
||||
}
|
||||
|
||||
@@ -117,6 +119,15 @@ class ZEmail extends Model
|
||||
$outlook_usernames = $settings['outlookUsernames'] ?? [];
|
||||
$domains = $settings['domains'] ?? [];
|
||||
|
||||
// Check username length limits
|
||||
$min_length = $settings['custom_username_length_min'] ?? 3;
|
||||
$max_length = $settings['custom_username_length_max'] ?? 20;
|
||||
|
||||
if (strlen($username) < $min_length || strlen($username) > $max_length) {
|
||||
$zemail = new ZEmail;
|
||||
$username = $zemail->generateRandomUsername();
|
||||
}
|
||||
|
||||
if (in_array($username, $forbidden_ids)) {
|
||||
return ZEmail::generateRandomEmail(true);
|
||||
}
|
||||
@@ -129,7 +140,7 @@ class ZEmail extends Model
|
||||
return ZEmail::generateRandomOutlook(true);
|
||||
}
|
||||
|
||||
$zemail = new ZEmail();
|
||||
$zemail = new ZEmail;
|
||||
|
||||
if ($username === '' && in_array($domain, $domains)) {
|
||||
return $zemail->generateRandomUsername().'@'.$domain;
|
||||
@@ -140,14 +151,15 @@ class ZEmail extends Model
|
||||
[$check_username, $post_username] = explode('+', $username, 2);
|
||||
|
||||
if (in_array($check_username, $outlook_usernames)) {
|
||||
$email = $username . '@' . $domain;
|
||||
$email = $username.'@'.$domain;
|
||||
} else {
|
||||
$email = $zemail->getRandomOutlookUser() . '+' . $post_username . '@' . $domain;
|
||||
$email = $zemail->getRandomOutlookUser().'+'.$post_username.'@'.$domain;
|
||||
}
|
||||
} else {
|
||||
$email = $zemail->getRandomOutlookUser() . '+' . $username . '@' . $domain;
|
||||
$email = $zemail->getRandomOutlookUser().'+'.$username.'@'.$domain;
|
||||
}
|
||||
ZEmail::storeEmail($email);
|
||||
|
||||
return $email;
|
||||
}
|
||||
|
||||
@@ -156,125 +168,130 @@ class ZEmail extends Model
|
||||
[$check_username, $post_username] = explode('+', $username, 2);
|
||||
|
||||
if (in_array($check_username, $gmail_usernames)) {
|
||||
$email = $username . '@' . $domain;
|
||||
$email = $username.'@'.$domain;
|
||||
} else {
|
||||
$email = $zemail->getRandomGmailUser() . '+' . $post_username . '@' . $domain;
|
||||
$email = $zemail->getRandomGmailUser().'+'.$post_username.'@'.$domain;
|
||||
}
|
||||
|
||||
} elseif (str_contains($username, '.')) {
|
||||
$check_username = str_replace('.', '', $username);
|
||||
|
||||
if (in_array($check_username, $gmail_usernames)) {
|
||||
$email = $username . '@' . $domain;
|
||||
$email = $username.'@'.$domain;
|
||||
} else {
|
||||
$email = $zemail->generateRandomGmail() . '@' . $domain;
|
||||
$email = $zemail->generateRandomGmail().'@'.$domain;
|
||||
}
|
||||
|
||||
} else {
|
||||
$email = $zemail->getRandomGmailUser() . '+' . $username . '@' . $domain;
|
||||
$email = $zemail->getRandomGmailUser().'+'.$username.'@'.$domain;
|
||||
}
|
||||
|
||||
ZEmail::storeEmail($email);
|
||||
|
||||
return $email;
|
||||
}
|
||||
|
||||
// Handle other custom domains
|
||||
if (!in_array($domain, $domains)) {
|
||||
if (! in_array($domain, $domains)) {
|
||||
return ZEmail::generateRandomEmail(true);
|
||||
}
|
||||
|
||||
$finalDomain = in_array($domain, $domains) ? $domain : ($domains[0] ?? 'example.com');
|
||||
$email = $username . '@' . $finalDomain;
|
||||
$email = $username.'@'.$finalDomain;
|
||||
|
||||
ZEmail::storeEmail($email);
|
||||
|
||||
return $email;
|
||||
}
|
||||
|
||||
|
||||
public static function generateRandomEmail($store = true): string
|
||||
{
|
||||
$zemail = new ZEmail();
|
||||
$zemail = new ZEmail;
|
||||
$domain = $zemail->getRandomDomain();
|
||||
if ($domain == "gmail.com") {
|
||||
$rd = mt_rand(0,1);
|
||||
if ($domain == 'gmail.com') {
|
||||
$rd = mt_rand(0, 1);
|
||||
if ($rd == 0) {
|
||||
$email = $zemail->generateRandomGmail();
|
||||
} else {
|
||||
$email = $zemail->getRandomGmailUser().'+'.$zemail->generateRandomUsername().'@gmail.com';
|
||||
}
|
||||
} elseif ($domain == "googlemail.com") {
|
||||
$rd = mt_rand(0,1);
|
||||
} elseif ($domain == 'googlemail.com') {
|
||||
$rd = mt_rand(0, 1);
|
||||
if ($rd == 0) {
|
||||
$email = $zemail->generateRandomGmail();
|
||||
} else {
|
||||
$email = $zemail->getRandomGmailUser().'+'.$zemail->generateRandomUsername().'@googlemail.com';
|
||||
}
|
||||
} elseif ($domain == "outlook.com") {
|
||||
} elseif ($domain == 'outlook.com') {
|
||||
$email = $zemail->getRandomOutlookUser().'+'.$zemail->generateRandomUsername().'@outlook.com';
|
||||
}
|
||||
else {
|
||||
$email = $zemail->generateRandomUsername() . '@' . $domain;
|
||||
} else {
|
||||
$email = $zemail->generateRandomUsername().'@'.$domain;
|
||||
}
|
||||
if ($store) {
|
||||
ZEmail::storeEmail($email);
|
||||
}
|
||||
|
||||
return $email;
|
||||
}
|
||||
|
||||
public static function generateRandomGmail($store = true): string
|
||||
{
|
||||
$zemail = new ZEmail();
|
||||
$zemail = new ZEmail;
|
||||
$uname = $zemail->getRandomGmailUser();
|
||||
$uname_len = strlen($uname);
|
||||
$len_power = $uname_len - 1;
|
||||
$combination = pow(2,$len_power);
|
||||
$rand_comb = mt_rand(1,$combination);
|
||||
$formatted = implode(' ',str_split($uname));
|
||||
$combination = pow(2, $len_power);
|
||||
$rand_comb = mt_rand(1, $combination);
|
||||
$formatted = implode(' ', str_split($uname));
|
||||
$uname_exp = explode(' ', $formatted);
|
||||
|
||||
$bin = intval("");
|
||||
for($i=0; $i<$len_power; $i++) {
|
||||
$bin .= mt_rand(0,1);
|
||||
$bin = intval('');
|
||||
for ($i = 0; $i < $len_power; $i++) {
|
||||
$bin .= mt_rand(0, 1);
|
||||
}
|
||||
$bin = explode(' ', implode(' ',str_split(strval($bin))));
|
||||
$bin = explode(' ', implode(' ', str_split(strval($bin))));
|
||||
|
||||
$email = "";
|
||||
for($i=0; $i<$len_power; $i++) {
|
||||
$email = '';
|
||||
for ($i = 0; $i < $len_power; $i++) {
|
||||
$email .= $uname_exp[$i];
|
||||
if($bin[$i]) {
|
||||
$email .= ".";
|
||||
if ($bin[$i]) {
|
||||
$email .= '.';
|
||||
}
|
||||
}
|
||||
$email .= $uname_exp[$i];
|
||||
$gmail_rand = mt_rand(1,10);
|
||||
if($gmail_rand > 5) {
|
||||
$email .= "@gmail.com";
|
||||
$gmail_rand = mt_rand(1, 10);
|
||||
if ($gmail_rand > 5) {
|
||||
$email .= '@gmail.com';
|
||||
} else {
|
||||
$email .= "@googlemail.com";
|
||||
$email .= '@googlemail.com';
|
||||
}
|
||||
if ($store) {
|
||||
ZEmail::storeEmail($email);
|
||||
}
|
||||
|
||||
return $email;
|
||||
}
|
||||
|
||||
public static function generateRandomOutlook($store = true): string
|
||||
{
|
||||
$zemail = new ZEmail();
|
||||
$zemail = new ZEmail;
|
||||
$email = $zemail->getRandomOutlookUser().'+'.$zemail->generateRandomUsername().'@outlook.com';
|
||||
if ($store) {
|
||||
ZEmail::storeEmail($email);
|
||||
}
|
||||
|
||||
return $email;
|
||||
}
|
||||
|
||||
private static function storeEmail($email): void
|
||||
{
|
||||
Log::create([
|
||||
'ip' => request()->ip(),
|
||||
'email' => $email
|
||||
'email' => $email,
|
||||
]);
|
||||
Cookie::queue('email', $email, 43800);
|
||||
$emails = Cookie::has('emails') ? unserialize(Cookie::get('emails')) : [];
|
||||
if (!in_array($email, $emails)) {
|
||||
if (! in_array($email, $emails)) {
|
||||
ZEmail::incrementEmailStats();
|
||||
$emails[] = $email;
|
||||
Cookie::queue('emails', serialize($emails), 43800);
|
||||
@@ -294,8 +311,6 @@ class ZEmail extends Model
|
||||
Meta::incrementMessagesReceived($count);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private function generateRandomUsername(): string
|
||||
{
|
||||
$start = json_decode(config('app.settings.configuration_settings'))->random_username_length_min ?? 0;
|
||||
@@ -303,45 +318,57 @@ class ZEmail extends Model
|
||||
if ($start == 0 && $end == 0) {
|
||||
return $this->generatePronounceableWord();
|
||||
}
|
||||
|
||||
return $this->generatedRandomBetweenLength($start, $end);
|
||||
}
|
||||
|
||||
protected function generatedRandomBetweenLength($start, $end): string
|
||||
{
|
||||
$length = rand($start, $end);
|
||||
|
||||
return $this->generateRandomString($length);
|
||||
}
|
||||
private function getRandomDomain() {
|
||||
|
||||
private function getRandomDomain()
|
||||
{
|
||||
$domains = json_decode(config('app.settings.configuration_settings'))->domains ?? [];
|
||||
$count = count($domains);
|
||||
|
||||
return $count > 0 ? $domains[rand(1, $count) - 1] : '';
|
||||
}
|
||||
|
||||
private function getRandomGmailUser() {
|
||||
private function getRandomGmailUser()
|
||||
{
|
||||
$gmailusername = json_decode(config('app.settings.configuration_settings'))->gmailUsernames ?? [];
|
||||
|
||||
$count = count($gmailusername);
|
||||
|
||||
return $count > 0 ? $gmailusername[rand(1, $count) - 1] : '';
|
||||
}
|
||||
private function getRandomOutlookUser() {
|
||||
|
||||
private function getRandomOutlookUser()
|
||||
{
|
||||
$outlook_username = json_decode(config('app.settings.configuration_settings'))->outlookUsernames ?? [];
|
||||
$count = count($outlook_username);
|
||||
|
||||
return $count > 0 ? $outlook_username[rand(1, $count) - 1] : '';
|
||||
}
|
||||
|
||||
private function generatePronounceableWord(): string
|
||||
{
|
||||
$c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones
|
||||
$v = 'aeiou'; //vowels
|
||||
$a = $c . $v; //both
|
||||
$c = 'bcdfghjklmnprstvwz'; // consonants except hard to speak ones
|
||||
$v = 'aeiou'; // vowels
|
||||
$a = $c.$v; // both
|
||||
$random = '';
|
||||
for ($j = 0; $j < 2; $j++) {
|
||||
$random .= $c[rand(0, strlen($c) - 1)];
|
||||
$random .= $v[rand(0, strlen($v) - 1)];
|
||||
$random .= $a[rand(0, strlen($a) - 1)];
|
||||
}
|
||||
|
||||
return $random;
|
||||
}
|
||||
|
||||
private function generateRandomString($length = 10): string
|
||||
{
|
||||
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
|
||||
@@ -350,7 +377,7 @@ class ZEmail extends Model
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
||||
}
|
||||
|
||||
return $randomString;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user