ObfuscateHelperTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Tools\Test\TestCase\View\Helper;
  3. use Cake\View\View;
  4. use Shim\TestSuite\TestCase;
  5. use Tools\View\Helper\ObfuscateHelper;
  6. class ObfuscateHelperTest extends TestCase {
  7. /**
  8. * @var \Tools\View\Helper\ObfuscateHelper
  9. */
  10. protected $Obfuscate;
  11. /**
  12. * @return void
  13. */
  14. public function setUp(): void {
  15. parent::setUp();
  16. $this->Obfuscate = new ObfuscateHelper(new View());
  17. }
  18. /**
  19. * @return void
  20. */
  21. public function tearDown(): void {
  22. unset($this->Table);
  23. parent::tearDown();
  24. }
  25. /**
  26. * @return void
  27. */
  28. public function testObject() {
  29. $this->assertInstanceOf('Tools\View\Helper\ObfuscateHelper', $this->Obfuscate);
  30. }
  31. /**
  32. * @return void
  33. */
  34. public function testEncodeEmail() {
  35. $result = $this->Obfuscate->encodeEmail('foobar@somedomain.com');
  36. $expected = '<span>@</span>';
  37. $this->assertStringContainsString($expected, $result);
  38. }
  39. /**
  40. * @return void
  41. */
  42. public function testEncodeEmailUrl() {
  43. $result = $this->Obfuscate->encodeEmailUrl('foobar@somedomain.com');
  44. $expected = '<script language=javascript>';
  45. $this->assertStringContainsString($expected, $result);
  46. }
  47. /**
  48. * @return void
  49. */
  50. public function testEncodeText() {
  51. $result = $this->Obfuscate->encodeText('foobar@somedomain.com');
  52. $expected = ';&#';
  53. $this->assertStringContainsString($expected, $result);
  54. }
  55. /**
  56. * @return void
  57. */
  58. public function testHideEmail() {
  59. $mails = [
  60. 'test@test.de' => 't..t@t..t.de',
  61. 'xx@yy.de' => 'x..x@y..y.de',
  62. 'erk-wf@ve-eeervdg.com' => 'e..f@v..g.com',
  63. ];
  64. foreach ($mails as $mail => $expected) {
  65. $res = $this->Obfuscate->hideEmail($mail);
  66. //echo '\''.$mail.'\' becomes \''.$res.'\' - expected \''.$expected.'\'';
  67. $this->assertEquals($expected, $res);
  68. }
  69. }
  70. /**
  71. * @return void
  72. */
  73. public function testWordCensor() {
  74. $data = [
  75. 'dfssdfsdj sdkfj sdkfj ksdfj bitch ksdfj' => 'dfssdfsdj sdkfj sdkfj ksdfj ##### ksdfj',
  76. '122 jsdf ficken Sjdkf sdfj sdf' => '122 jsdf ###### Sjdkf sdfj sdf',
  77. '122 jsdf FICKEN sjdkf sdfjs sdf' => '122 jsdf ###### sjdkf sdfjs sdf',
  78. 'dddddddddd ARSCH ddddddddddddd' => 'dddddddddd ##### ddddddddddddd',
  79. //'\';alert(String.fromCharCode(88,83,83))//\';alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//\";alert(String.fromCharCode(88,83,83))//--></SCRIPT>">\'><SCRIPT>alert(String.fromCharCode(88,83,83))</SCRIPT>' => null
  80. ];
  81. foreach ($data as $value => $expected) {
  82. $res = $this->Obfuscate->wordCensor($value, ['Arsch', 'Ficken', 'Bitch']);
  83. //debug('\''.h($value).'\' becomes \''.h($res).'\'', null, false);
  84. $this->assertEquals($expected ?? $value, $res);
  85. }
  86. }
  87. }