|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Mpdf; |
| 4 | + |
| 5 | +/** |
| 6 | + * A select is laid out as many rows tall as HTML would draw it: a multiple select with no size is a list box four |
| 7 | + * rows tall, and an explicit size is kept (GravityPDF/mpdf#425) |
| 8 | + */ |
| 9 | +class SelectRowsTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase |
| 10 | +{ |
| 11 | + use PageStreams; |
| 12 | + |
| 13 | + /** |
| 14 | + * A select's attributes, the attributes of a select it should be as tall as, and whether forms are active |
| 15 | + * |
| 16 | + * @return mixed[] |
| 17 | + */ |
| 18 | + public function selects() |
| 19 | + { |
| 20 | + $selects = [ |
| 21 | + 'multiple with no size' => ['multiple', 'size="4"'], |
| 22 | + 'multiple="multiple" with no size' => ['multiple="multiple"', 'size="4"'], |
| 23 | + 'multiple with a size of zero' => ['multiple size="0"', 'size="4"'], |
| 24 | + 'multiple with a size of one' => ['multiple size="1"', ''], |
| 25 | + 'multiple with a size of two' => ['multiple size="2"', 'size="2"'], |
| 26 | + 'multiple with a size of six' => ['multiple size="6"', 'size="6"'], |
| 27 | + 'no size' => ['', 'size="1"'], |
| 28 | + ]; |
| 29 | + |
| 30 | + $cases = []; |
| 31 | + foreach ($selects as $name => $select) { |
| 32 | + $cases[$name . ', static'] = [$select[0], $select[1], false]; |
| 33 | + $cases[$name . ', active'] = [$select[0], $select[1], true]; |
| 34 | + } |
| 35 | + |
| 36 | + return $cases; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @dataProvider selects |
| 41 | + * |
| 42 | + * @param string $attributes |
| 43 | + * @param string $sameAs |
| 44 | + * @param bool $active |
| 45 | + */ |
| 46 | + public function testASelectIsAsTallAsTheRowsHtmlGivesIt($attributes, $sameAs, $active) |
| 47 | + { |
| 48 | + $this->assertEqualsWithDelta($this->height($sameAs, $active), $this->height($attributes, $active), 0.001); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * A multiple select with no size is taller than a drop-down in both modes |
| 53 | + */ |
| 54 | + public function testAMultipleSelectWithNoSizeIsTallerThanADropDown() |
| 55 | + { |
| 56 | + $this->assertGreaterThan($this->height('', false) + 1, $this->height('multiple', false)); |
| 57 | + $this->assertGreaterThan($this->height('', true) + 1, $this->height('multiple', true)); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * An active multiple select with no size gets a widget four rows tall |
| 62 | + */ |
| 63 | + public function testAnActiveMultipleSelectWithNoSizeHasAWidgetFourRowsTall() |
| 64 | + { |
| 65 | + $this->assertEqualsWithDelta($this->widgetHeight('size="4"'), $this->widgetHeight('multiple'), 0.001); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * How far a line holding only the select advances the page, in mm |
| 70 | + * |
| 71 | + * @param string $attributes |
| 72 | + * @param bool $active |
| 73 | + * |
| 74 | + * @return float |
| 75 | + */ |
| 76 | + private function height($attributes, $active) |
| 77 | + { |
| 78 | + $mpdf = $this->mpdf(['useActiveForms' => $active]); |
| 79 | + $top = $mpdf->y; |
| 80 | + $mpdf->WriteHTML($this->select($attributes)); |
| 81 | + |
| 82 | + return $mpdf->y - $top; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * The height of the active select's widget /Rect, in points |
| 87 | + * |
| 88 | + * @param string $attributes |
| 89 | + * |
| 90 | + * @return float |
| 91 | + */ |
| 92 | + private function widgetHeight($attributes) |
| 93 | + { |
| 94 | + $pdf = $this->render($this->select($attributes), ['useActiveForms' => true]); |
| 95 | + $this->assertSame(1, preg_match('/\/Rect \[\s*[\d.]+ ([\d.]+) [\d.]+ ([\d.]+)\s*\][^>]*?\/FT \/Ch\b/s', $pdf, $rect), 'The select should be written as a choice field'); |
| 96 | + |
| 97 | + return $rect[2] - $rect[1]; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * A form holding one select with two options |
| 102 | + * |
| 103 | + * @param string $attributes |
| 104 | + * |
| 105 | + * @return string |
| 106 | + */ |
| 107 | + private function select($attributes) |
| 108 | + { |
| 109 | + return '<form><select name="choice" ' . $attributes . '><option>A</option><option>B</option></select></form>'; |
| 110 | + } |
| 111 | +} |
0 commit comments