|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DataKit\DataViews\Tests\DataView; |
| 4 | + |
| 5 | +use DataKit\DataViews\DataView\Action; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +/** |
| 9 | + * Unit tests for {@see Action} |
| 10 | + * |
| 11 | + * @since $ver$ |
| 12 | + */ |
| 13 | +final class ActionTest extends TestCase { |
| 14 | + /** |
| 15 | + * Test case for {@see Action::url()}. |
| 16 | + * |
| 17 | + * @since $ver$ |
| 18 | + */ |
| 19 | + public function test_url(): void { |
| 20 | + $action = Action::url( 'some-action', 'Action', 'url' ); |
| 21 | + $array = $action->to_array(); |
| 22 | + |
| 23 | + self::assertStringContainsString( 'datakit_dataviews_actions.url', $array['callback'] ?? '' ); |
| 24 | + self::assertStringContainsString( '"type":"url"', $array['callback'] ?? '' ); |
| 25 | + self::assertSame( 'some-action', $array['id'] ); |
| 26 | + self::assertSame( 'Action', $array['label'] ); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Test case for {@see Action::modal()}. |
| 31 | + * |
| 32 | + * @since $ver$ |
| 33 | + */ |
| 34 | + public function test_modal(): void { |
| 35 | + $modal = Action::modal( 'modal-action', 'Modal Action', 'some-url', true ); |
| 36 | + $array = $modal->to_array(); |
| 37 | + self::assertNull( $array['callback'] ?? null ); |
| 38 | + self::assertSame( 'modal-action', $array['id'] ); |
| 39 | + self::assertSame( 'Modal Action', $array['label'] ); |
| 40 | + |
| 41 | + $modal_callback = $array['RenderModal'] ?? ''; |
| 42 | + self::assertStringContainsString( 'datakit_modal(', $modal_callback ); |
| 43 | + self::assertStringContainsString( '{"url":"some-url","id":"modal-action","type":"modal"}', $modal_callback ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Test case for {@see Action::ajax()}. |
| 48 | + * |
| 49 | + * @since $ver$ |
| 50 | + */ |
| 51 | + public function test_ajax(): void { |
| 52 | + $ajax = Action::ajax( 'ajax-action', |
| 53 | + 'Ajax Action', |
| 54 | + 'some-url', |
| 55 | + 'PUT', |
| 56 | + [ 'extra' => 'param' ], |
| 57 | + true ); |
| 58 | + $put = $ajax->to_array(); |
| 59 | + $ajax = Action::ajax( 'ajax-action', 'Ajax Action', 'some-url', 'POST', [] ); |
| 60 | + $post = $ajax->to_array(); |
| 61 | + |
| 62 | + self::assertStringContainsString( 'datakit_dataviews_actions.url', $put['callback'] ?? '' ); |
| 63 | + self::assertStringContainsString( '"type":"ajax"', $put['callback'] ?? '' ); |
| 64 | + self::assertStringContainsString( '"method":"PUT"', $put['callback'] ?? '' ); |
| 65 | + self::assertStringContainsString( '"params":{"extra":"param"}', $put['callback'] ?? '' ); |
| 66 | + self::assertStringContainsString( '"use_single_request":true', $put['callback'] ?? '' ); |
| 67 | + self::assertSame( 'ajax-action', $put['id'] ); |
| 68 | + self::assertSame( 'Ajax Action', $put['label'] ); |
| 69 | + |
| 70 | + self::assertStringContainsString( '"method":"POST"', $post['callback'] ?? '' ); |
| 71 | + self::assertStringContainsString( '"params":[]', $post['callback'] ?? '' ); |
| 72 | + self::assertStringContainsString( '"use_single_request":false', $post['callback'] ?? '' ); |
| 73 | + self::assertSame( 'ajax-action', $post['id'] ); |
| 74 | + self::assertSame( 'Ajax Action', $post['label'] ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test case for the basic action modifiers. |
| 79 | + * |
| 80 | + * @since $ver$ |
| 81 | + */ |
| 82 | + public function test_modifiers(): void { |
| 83 | + $action = Action::url( 'some-action', 'Action', 'url' ); |
| 84 | + $destructive_primary_bulk = $action->destructive()->primary( 'action-icon' )->bulk(); |
| 85 | + |
| 86 | + self::assertFalse( $action->to_array()['isDestructive'] ); |
| 87 | + self::assertFalse( $action->to_array()['isPrimary'] ); |
| 88 | + self::assertFalse( $action->to_array()['supportsBulk'] ); |
| 89 | + self::assertEmpty( $action->to_array()['icon'] ); |
| 90 | + |
| 91 | + self::assertTrue( $destructive_primary_bulk->to_array()['isDestructive'] ); |
| 92 | + self::assertTrue( $destructive_primary_bulk->to_array()['isPrimary'] ); |
| 93 | + self::assertTrue( $destructive_primary_bulk->to_array()['supportsBulk'] ); |
| 94 | + self::assertSame( 'action-icon', $destructive_primary_bulk->to_array()['icon'] ); |
| 95 | + |
| 96 | + $normal_secondary_single = $destructive_primary_bulk->not_destructive()->single()->secondary(); |
| 97 | + |
| 98 | + self::assertFalse( $normal_secondary_single->to_array()['isDestructive'] ); |
| 99 | + self::assertFalse( $normal_secondary_single->to_array()['isPrimary'] ); |
| 100 | + self::assertFalse( $normal_secondary_single->to_array()['supportsBulk'] ); |
| 101 | + self::assertEmpty( $normal_secondary_single->to_array()['icon'] ); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Test case for {@see Action::requires()}. |
| 106 | + * |
| 107 | + * @since $ver$ |
| 108 | + */ |
| 109 | + public function test_requires(): void { |
| 110 | + $action = Action::url( 'some-action', 'Action', 'url' )->requires( [ 'url', 'other' ] ); |
| 111 | + $empty = $action->requires( [] ); |
| 112 | + self::assertStringContainsString( '["url","other"].every(', $action->to_array()['isEligible'] ); |
| 113 | + self::assertNull( $empty->to_array()['isEligible'] ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Test case for {@see Action::confirm()}. |
| 118 | + * |
| 119 | + * @since $ver$ |
| 120 | + */ |
| 121 | + public function test_confirm(): void { |
| 122 | + $action = Action::url( 'some-action', 'Action', 'url' ); |
| 123 | + $confirm = $action->confirm( 'sure?' ); |
| 124 | + self::assertStringNotContainsString( 'confirm', $action->to_array()['callback'] ); |
| 125 | + self::assertStringContainsString( '"confirm":"sure?"', $confirm->to_array()['callback'] ); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Test case for {@see Action::allow_scripts()} and {@see Action::deny_scripts()}. |
| 130 | + * |
| 131 | + * @since $ver$ |
| 132 | + */ |
| 133 | + public function test_scripts(): void { |
| 134 | + $allowed = Action::url( 'some-action', 'Action', 'url' )->allow_scripts(); |
| 135 | + $denied = Action::url( 'some-action', 'Action', 'url' )->deny_scripts(); |
| 136 | + self::assertStringContainsString( '"is_scripts_allowed":true', $allowed->to_array()['callback'] ); |
| 137 | + self::assertStringContainsString( '"is_scripts_allowed":false', $denied->to_array()['callback'] ); |
| 138 | + } |
| 139 | +} |
0 commit comments