PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 19, 2022

[FIXED] Laravel 5: assertSeeText() and html entities

 January 19, 2022     laravel-5.5, php, phpunit     No comments   

Issue

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.


Solution

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))


Answered By - SystemGlitch
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing