|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | +namespace Soatok\Cupcake\Tests; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Soatok\Cupcake\Form; |
| 7 | +use Soatok\Cupcake\AutoID; |
| 8 | +use Soatok\Cupcake\Ingredients\Div; |
| 9 | +use Soatok\Cupcake\Ingredients\Input\Text; |
| 10 | + |
| 11 | +/** |
| 12 | + * @covers \Soatok\Cupcake\AutoID |
| 13 | + */ |
| 14 | +class AutoIDTest extends TestCase |
| 15 | +{ |
| 16 | + public function testEmpty() |
| 17 | + { |
| 18 | + $form = new Form(); |
| 19 | + $form->disableAntiCSRF(); |
| 20 | + $autoId = new AutoID(str_repeat("\0", 32)); |
| 21 | + |
| 22 | + // Default behavior: |
| 23 | + $this->assertSame( |
| 24 | + '<form id="cupcake-b5e166965cf1713adfda5de4b6c52228" method="GET" action=""></form>', |
| 25 | + $autoId->autoPopulate($form) . '' |
| 26 | + ); |
| 27 | + |
| 28 | + // This always mixes with spl_object_hash even with weak keys |
| 29 | + $autoId->setObjectHash(true); |
| 30 | + |
| 31 | + $this->assertNotSame( |
| 32 | + '<form id="cupcake-b5e166965cf1713adfda5de4b6c52228" method="GET" action=""></form>', |
| 33 | + $autoId->autoPopulate($form) . '' |
| 34 | + ); |
| 35 | + |
| 36 | + $expect = $autoId->autoId(pack('P', 0) . spl_object_hash($autoId)); |
| 37 | + $this->assertSame( |
| 38 | + '<form id="' . $expect . '" method="GET" action=""></form>', |
| 39 | + $autoId->autoPopulate($form) . '' |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + public function testFormWithZeroKey() |
| 44 | + { |
| 45 | + $form = $this->getDummyForm(); |
| 46 | + $autoId = new AutoID(str_repeat("\0", 32)); |
| 47 | + |
| 48 | + $this->assertSame( |
| 49 | + '<form id="cupcake-b5e166965cf1713adfda5de4b6c52228" method="GET" action=""><input id="cupcake-2d7603abf000d275112cbe001b7ff4f1" type="text" name="foo" /><div id="cupcake-481ad0fd1b490065721c82859ee4e00b"><input id="cupcake-6f90b0f6691dfb0521222b0a9312366f" type="text" name="bar" /></div></form>', |
| 50 | + $autoId->autoPopulate($form) . '' |
| 51 | + ); |
| 52 | + |
| 53 | + $autoId->setObjectHash(true); |
| 54 | + $this->assertNotSame( |
| 55 | + '<form id="cupcake-b5e166965cf1713adfda5de4b6c52228" method="GET" action=""><input id="cupcake-2d7603abf000d275112cbe001b7ff4f1" type="text" name="foo" /><div id="cupcake-481ad0fd1b490065721c82859ee4e00b"><input id="cupcake-6f90b0f6691dfb0521222b0a9312366f" type="text" name="bar" /></div></form>', |
| 56 | + $autoId->autoPopulate($form) . '' |
| 57 | + ); |
| 58 | + |
| 59 | + $h0 = $autoId->autoId(pack('P', 0) . spl_object_hash($autoId)); |
| 60 | + $h1 = $autoId->autoId(pack('P', 1) . spl_object_hash($autoId) . pack('P', 0)); |
| 61 | + $h2 = $autoId->autoId(pack('P', 1) . spl_object_hash($autoId) . pack('P', 1)); |
| 62 | + $h3 = $autoId->autoId(pack('P', 2) . spl_object_hash($autoId) . pack('P', 1) . pack('P', 0)); |
| 63 | + |
| 64 | + $this->assertSame( |
| 65 | + '<form id="' . $h0 |
| 66 | + . '" method="GET" action=""><input id="' . $h1 |
| 67 | + . '" type="text" name="foo" /><div id="' . $h2 |
| 68 | + . '"><input id="' . $h3 . |
| 69 | + '" type="text" name="bar" /></div></form>', |
| 70 | + $autoId->autoPopulate($form) . '' |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + private function getDummyForm(): Form |
| 75 | + { |
| 76 | + $form = new Form(); |
| 77 | + $form->disableAntiCSRF(); |
| 78 | + $form->append(new Text('foo')); |
| 79 | + $form->append( |
| 80 | + (new Div())->append(new Text('bar')) |
| 81 | + ); |
| 82 | + return $form; |
| 83 | + } |
| 84 | +} |
0 commit comments