Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5: assertSeeText() and html entities

I'm using PHP Faker to generate random data (using factories) in the database and sometimes it generates apostrophes.

In my test, I use assertSeeText(). The problem is that when the tested string contains special characters such as apostrophes, these characters are converted to html entities in the view so the assertion is false.

$siteShow = factory(Site::class)->create();
$this->get('admin/site/'. $siteShow->id_site)
        ->assertStatus(200)
        ->assertSeeText($siteShow->name)
        ->assertSeeText($siteShow->address)

Example case when the assertions fail: $siteShow->name equals O'Neil. It fails because in the view it appears as O'Neil.

My question is: how do I make it so that the assertion is true in both cases: the name in the view is O'Neil or O'Neil? I would like the solution to handle any html entity if possible. Thank you in advance.

like image 321
SystemGlitch Avatar asked Mar 30 '18 08:03

SystemGlitch


2 Answers

I found a solution. As my views are generated by Blade using the {{ ... }} syntax, the text is escaped and passed through htmlspecialchars() and if I'm correct, more especially through the e() helper. So I just did the following and it works:

$siteShow = factory(Site::class)->create();
$this->get('admin/site/'. $siteShow->id_site)
    ->assertStatus(200)
    ->assertSeeText(e($siteShow->name))
    ->assertSeeText(e($siteShow->address))
like image 145
SystemGlitch Avatar answered Oct 07 '22 00:10

SystemGlitch


SystemGlitch's answer didn't work for me and I think the best way is to use the second boolean parameter for assertSeeText and assertSee to disable escaping

$siteShow = factory(Site::class)->create();
$this->get('admin/site/'. $siteShow->id_site)
    ->assertSeeText($siteShow->name,false)
    ->assertSeeText($siteShow->address,false)

like image 5
Mahdi mehrabi Avatar answered Oct 07 '22 00:10

Mahdi mehrabi