-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added parseAsText to mention parser. Fix multiple email recipients
- Loading branch information
Showing
5 changed files
with
208 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,85 +2,175 @@ | |
|
||
use App\Open\MentionParser; | ||
|
||
test('it replaces mention elements with their corresponding values', function () { | ||
$content = '<p>Hello <span mention mention-field-id="123">Placeholder</span></p>'; | ||
$data = [['id' => '123', 'value' => 'World']]; | ||
describe('MentionParser', function () { | ||
it('replaces mentions with their values in HTML', function () { | ||
$content = '<div>Hello <span mention mention-field-id="123" mention-fallback="">Name</span></div>'; | ||
$data = [ | ||
['id' => '123', 'value' => 'John Doe'] | ||
]; | ||
|
||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
|
||
expect($result)->toBe('<div>Hello John Doe</div>'); | ||
}); | ||
|
||
it('uses fallback when value is not found', function () { | ||
$content = '<span mention mention-field-id="456" mention-fallback="Guest">Name</span>'; | ||
$data = []; | ||
|
||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
|
||
expect($result)->toBe('Guest'); | ||
}); | ||
|
||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
it('removes the element when no value and no fallback is provided', function () { | ||
$content = '<div>Hello <span mention mention-field-id="789" mention-fallback="">Name</span>!</div>'; | ||
$data = []; | ||
|
||
expect($result)->toBe('<p>Hello World</p>'); | ||
}); | ||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
|
||
test('it handles multiple mentions', function () { | ||
$content = '<p><span mention mention-field-id="123">Name</span> is <span mention mention-field-id="456">Age</span> years old</p>'; | ||
$data = [ | ||
['id' => '123', 'value' => 'John'], | ||
['id' => '456', 'value' => 30], | ||
]; | ||
expect($result)->toBe('<div>Hello !</div>'); | ||
}); | ||
|
||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
describe('parseAsText', function () { | ||
it('converts HTML to plain text with proper line breaks', function () { | ||
$content = '<div>First line</div><div>Second line</div>'; | ||
|
||
expect($result)->toBe('<p>John is 30 years old</p>'); | ||
}); | ||
$parser = new MentionParser($content, []); | ||
$result = $parser->parseAsText(); | ||
|
||
test('it uses fallback when value is not found', function () { | ||
$content = '<p>Hello <span mention mention-field-id="123" mention-fallback="Friend">Placeholder</span></p>'; | ||
$data = []; | ||
expect($result)->toBe("First line\nSecond line"); | ||
}); | ||
|
||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
it('handles email addresses with proper line breaks', function () { | ||
$content = '<span mention mention-field-id="123" mention-fallback="">Email</span><div>[email protected]</div>'; | ||
$data = [ | ||
['id' => '123', 'value' => '[email protected]'] | ||
]; | ||
|
||
expect($result)->toBe('<p>Hello Friend</p>'); | ||
}); | ||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parseAsText(); | ||
|
||
test('it removes mention element when no value and no fallback', function () { | ||
$content = '<p>Hello <span mention mention-field-id="123">Placeholder</span></p>'; | ||
$data = []; | ||
expect($result)->toBe("[email protected]\n[email protected]"); | ||
}); | ||
|
||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
it('handles multiple mentions and complex HTML structure', function () { | ||
$content = ' | ||
<div>Contact: <span mention mention-field-id="123" mention-fallback="">Email1</span></div> | ||
<div>CC: <span mention mention-field-id="456" mention-fallback="">Email2</span></div> | ||
<div>Additional: [email protected]</div> | ||
'; | ||
$data = [ | ||
['id' => '123', 'value' => '[email protected]'], | ||
['id' => '456', 'value' => '[email protected]'], | ||
]; | ||
|
||
expect($result)->toBe('<p>Hello </p>'); | ||
}); | ||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parseAsText(); | ||
|
||
test('it handles array values', function () { | ||
$content = '<p>Tags: <span mention mention-field-id="123">Placeholder</span></p>'; | ||
$data = [['id' => '123', 'value' => ['PHP', 'Laravel', 'Testing']]]; | ||
expect($result)->toBe( | ||
"Contact: [email protected]\n" . | ||
"CC: [email protected]\n" . | ||
"Additional: [email protected]" | ||
); | ||
}); | ||
|
||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
it('handles array values in mentions', function () { | ||
$content = '<span mention mention-field-id="123" mention-fallback="">Emails</span>'; | ||
$data = [ | ||
['id' => '123', 'value' => ['[email protected]', '[email protected]']] | ||
]; | ||
|
||
expect($result)->toBe('<p>Tags: PHP, Laravel, Testing</p>'); | ||
}); | ||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parseAsText(); | ||
|
||
test('it preserves HTML structure', function () { | ||
$content = '<div><p>Hello <span mention mention-field-id="123">Placeholder</span></p><p>How are you?</p></div>'; | ||
$data = [['id' => '123', 'value' => 'World']]; | ||
expect($result)->toBe('[email protected], [email protected]'); | ||
}); | ||
}); | ||
|
||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
test('it replaces mention elements with their corresponding values', function () { | ||
$content = '<p>Hello <span mention mention-field-id="123">Placeholder</span></p>'; | ||
$data = [['id' => '123', 'value' => 'World']]; | ||
|
||
expect($result)->toBe('<div><p>Hello World</p><p>How are you?</p></div>'); | ||
}); | ||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
|
||
test('it handles UTF-8 characters', function () { | ||
$content = '<p>こんにちは <span mention mention-field-id="123">Placeholder</span></p>'; | ||
$data = [['id' => '123', 'value' => '世界']]; | ||
expect($result)->toBe('<p>Hello World</p>'); | ||
}); | ||
|
||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
test('it handles multiple mentions', function () { | ||
$content = '<p><span mention mention-field-id="123">Name</span> is <span mention mention-field-id="456">Age</span> years old</p>'; | ||
$data = [ | ||
['id' => '123', 'value' => 'John'], | ||
['id' => '456', 'value' => 30], | ||
]; | ||
|
||
expect($result)->toBe('<p>こんにちは 世界</p>'); | ||
}); | ||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
|
||
expect($result)->toBe('<p>John is 30 years old</p>'); | ||
}); | ||
|
||
test('it uses fallback when value is not found', function () { | ||
$content = '<p>Hello <span mention mention-field-id="123" mention-fallback="Friend">Placeholder</span></p>'; | ||
$data = []; | ||
|
||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
|
||
expect($result)->toBe('<p>Hello Friend</p>'); | ||
}); | ||
|
||
test('it removes mention element when no value and no fallback', function () { | ||
$content = '<p>Hello <span mention mention-field-id="123">Placeholder</span></p>'; | ||
$data = []; | ||
|
||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
|
||
expect($result)->toBe('<p>Hello </p>'); | ||
}); | ||
|
||
test('it handles array values', function () { | ||
$content = '<p>Tags: <span mention mention-field-id="123">Placeholder</span></p>'; | ||
$data = [['id' => '123', 'value' => ['PHP', 'Laravel', 'Testing']]]; | ||
|
||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
|
||
expect($result)->toBe('<p>Tags: PHP, Laravel, Testing</p>'); | ||
}); | ||
|
||
test('it preserves HTML structure', function () { | ||
$content = '<div><p>Hello <span mention mention-field-id="123">Placeholder</span></p><p>How are you?</p></div>'; | ||
$data = [['id' => '123', 'value' => 'World']]; | ||
|
||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
|
||
expect($result)->toBe('<div><p>Hello World</p><p>How are you?</p></div>'); | ||
}); | ||
|
||
test('it handles UTF-8 characters', function () { | ||
$content = '<p>こんにちは <span mention mention-field-id="123">Placeholder</span></p>'; | ||
$data = [['id' => '123', 'value' => '世界']]; | ||
|
||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
|
||
expect($result)->toBe('<p>こんにちは 世界</p>'); | ||
}); | ||
|
||
test('it handles content without surrounding paragraph tags', function () { | ||
$content = 'some text <span contenteditable="false" mention="" mention-field-id="123" mention-field-name="Post excerpt" mention-fallback="">Post excerpt</span> dewde'; | ||
$data = [['id' => '123', 'value' => 'replaced text']]; | ||
test('it handles content without surrounding paragraph tags', function () { | ||
$content = 'some text <span contenteditable="false" mention="" mention-field-id="123" mention-field-name="Post excerpt" mention-fallback="">Post excerpt</span> dewde'; | ||
$data = [['id' => '123', 'value' => 'replaced text']]; | ||
|
||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
$parser = new MentionParser($content, $data); | ||
$result = $parser->parse(); | ||
|
||
expect($result)->toBe('some text replaced text dewde'); | ||
expect($result)->toBe('some text replaced text dewde'); | ||
}); | ||
}); |