|
8 | 8 | use lucatume\WPBrowser\TestCase\WPTestCase; |
9 | 9 | use ReflectionClass; |
10 | 10 | use WPGraphQL\Logging\Events\EventManager; |
11 | | - |
| 11 | +use WPGraphQL\Logging\Logger\Database\WordPressDatabaseEntity; |
| 12 | +use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab; |
| 13 | +use WPGraphQL\Logging\Admin\Settings\Fields\Tab\DataManagementTab; |
| 14 | +use WPGraphQL\Logging\Events\Events; |
| 15 | +use WPGraphQL\Logging\Admin\Settings\ConfigurationHelper; |
12 | 16 |
|
13 | 17 | /** |
14 | 18 | * Test for the Plugin |
@@ -55,6 +59,76 @@ public function test_clone_method_throws_error() { |
55 | 59 | $this->assertInstanceOf( Plugin::class, $clone ); |
56 | 60 | } |
57 | 61 |
|
| 62 | + public function test_plugin_activate() { |
| 63 | + |
| 64 | + // Delet configuration option |
| 65 | + $configuration = ConfigurationHelper::get_instance(); |
| 66 | + $option_key = $configuration->get_option_key(); |
| 67 | + delete_option( $option_key ); |
| 68 | + |
| 69 | + // Verify that the configuration option has been deleted |
| 70 | + $option_value = get_option( $option_key ); |
| 71 | + $this->assertEmpty( $option_value ); |
| 72 | + |
| 73 | + |
| 74 | + $plugin = Plugin::init(); |
| 75 | + $plugin::activate(); |
| 76 | + |
| 77 | + // Verify that the datatbase has been created |
| 78 | + global $wpdb; |
| 79 | + $table_name = WordPressDatabaseEntity::get_table_name(); |
| 80 | + $table_exists = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) ); |
| 81 | + $this->assertNotEmpty( $table_exists ); |
| 82 | + $this->assertEquals( $table_exists, $table_name ); |
| 83 | + |
| 84 | + // Verify that the default configuration has been set |
| 85 | + $option_value = $configuration->get_option_value( WPGRAPHQL_LOGGING_SETTINGS_KEY ); |
| 86 | + $this->assertNotEmpty( $option_value ); |
| 87 | + $default_configuration = [ |
| 88 | + BasicConfigurationTab::get_name() => [ |
| 89 | + BasicConfigurationTab::ENABLED => true, |
| 90 | + BasicConfigurationTab::EXCLUDE_QUERY => '__schema,GetSeedNode', // Exclude introspection and GetSeedNode queries. |
| 91 | + BasicConfigurationTab::DATA_SAMPLING => '10', |
| 92 | + BasicConfigurationTab::EVENT_LOG_SELECTION => [ |
| 93 | + Events::PRE_REQUEST, |
| 94 | + Events::BEFORE_GRAPHQL_EXECUTION, |
| 95 | + Events::BEFORE_RESPONSE_RETURNED, |
| 96 | + Events::REQUEST_DATA, |
| 97 | + Events::REQUEST_RESULTS, |
| 98 | + Events::RESPONSE_HEADERS_TO_SEND, |
| 99 | + ], |
| 100 | + BasicConfigurationTab::LOG_RESPONSE => false, |
| 101 | + ], |
| 102 | + DataManagementTab::get_name() => [ |
| 103 | + DataManagementTab::DATA_DELETION_ENABLED => true, |
| 104 | + DataManagementTab::DATA_RETENTION_DAYS => 7, |
| 105 | + DataManagementTab::DATA_SANITIZATION_ENABLED => true, |
| 106 | + DataManagementTab::DATA_SANITIZATION_METHOD => 'recommended', |
| 107 | + ], |
| 108 | + ]; |
| 109 | + $this->assertEquals( $option_value, $default_configuration ); |
| 110 | + } |
| 111 | + |
| 112 | + public function test_plugin_activate_when_configuration_already_exists() { |
| 113 | + $configuration = ConfigurationHelper::get_instance(); |
| 114 | + $option_key = $configuration->get_option_key(); |
| 115 | + $default_configuration = [ |
| 116 | + BasicConfigurationTab::get_name() => [ |
| 117 | + BasicConfigurationTab::ENABLED => true, |
| 118 | + ], |
| 119 | + ]; |
| 120 | + update_option( $option_key, $default_configuration ); |
| 121 | + |
| 122 | + |
| 123 | + $plugin = Plugin::init(); |
| 124 | + $plugin::activate(); |
| 125 | + |
| 126 | + // Verify that the default configuration has not been set |
| 127 | + $configuration = ConfigurationHelper::get_instance(); |
| 128 | + $option_value = $configuration->get_option_value( WPGRAPHQL_LOGGING_SETTINGS_KEY ); |
| 129 | + $this->assertEquals( $option_value, $default_configuration ); |
| 130 | + } |
| 131 | + |
58 | 132 | public function test_wakeup_method_throws_error() { |
59 | 133 | $reflection = new ReflectionClass( Plugin::class ); |
60 | 134 | $plugin = $reflection->newInstanceWithoutConstructor(); |
|
0 commit comments