7
7
namespace Magento \Setup ;
8
8
9
9
use Magento \Framework \App \ResourceConnection ;
10
- use Magento \Framework \Setup \Declaration \Schema \Diff \SchemaDiff ;
11
- use Magento \Framework \Setup \Declaration \Schema \SchemaConfigInterface ;
12
- use Magento \Framework \Setup \Declaration \Schema \Sharding ;
10
+ use Magento \Framework \Setup \Declaration \Schema \Db \DbSchemaReaderInterface ;
13
11
use Magento \TestFramework \Deploy \CliCommand ;
14
- use Magento \TestFramework \Deploy \DescribeTable ;
15
12
use Magento \TestFramework \Deploy \TestModuleManager ;
16
13
use Magento \TestFramework \Helper \Bootstrap ;
17
14
use Magento \TestFramework \TestCase \SetupTestCase ;
@@ -29,19 +26,28 @@ class SafeInstallerTest extends SetupTestCase
29
26
/**
30
27
* @var CliCommand
31
28
*/
32
- private $ cliCommad ;
29
+ private $ cliCommand ;
33
30
34
31
/**
35
32
* @var ResourceConnection
36
33
*/
37
34
private $ resourceConnection ;
38
35
36
+ /**
37
+ * @var DbSchemaReaderInterface
38
+ */
39
+ private $ dbSchemaReader ;
40
+
41
+ /**
42
+ * @inheritdoc
43
+ */
39
44
public function setUp ()
40
45
{
41
46
$ objectManager = Bootstrap::getObjectManager ();
42
47
$ this ->moduleManager = $ objectManager ->get (TestModuleManager::class);
43
- $ this ->cliCommad = $ objectManager ->get (CliCommand::class);
48
+ $ this ->cliCommand = $ objectManager ->get (CliCommand::class);
44
49
$ this ->resourceConnection = $ objectManager ->get (ResourceConnection::class);
50
+ $ this ->dbSchemaReader = $ objectManager ->get (DbSchemaReaderInterface::class);
45
51
}
46
52
47
53
/**
@@ -52,7 +58,7 @@ public function testInstallation()
52
58
{
53
59
$ testTableData = $ this ->getData ();
54
60
$ row = reset ($ testTableData );
55
- $ this ->cliCommad ->install (['Magento_TestSetupDeclarationModule4 ' ]);
61
+ $ this ->cliCommand ->install (['Magento_TestSetupDeclarationModule4 ' ]);
56
62
$ adapter = $ this ->resourceConnection ->getConnection ();
57
63
$ testTableName = $ this ->resourceConnection ->getTableName ('test_table ' );
58
64
$ adapter ->insertArray (
@@ -67,7 +73,7 @@ public function testInstallation()
67
73
'db_schema.xml ' ,
68
74
'etc '
69
75
);
70
- $ this ->cliCommad ->upgrade (
76
+ $ this ->cliCommand ->upgrade (
71
77
[
72
78
'safe-mode ' => true ,
73
79
]
@@ -79,12 +85,66 @@ public function testInstallation()
79
85
'db_schema.xml ' ,
80
86
'etc '
81
87
);
82
- $ this ->cliCommad ->upgrade (
88
+ $ this ->cliCommand ->upgrade (
83
89
[
84
90
'data-restore ' => true ,
85
91
]
86
92
);
87
93
$ testTableSelect = $ adapter ->select ()->from ($ testTableName );
88
94
self ::assertEquals ($ testTableData , $ adapter ->fetchAll ($ testTableSelect ));
89
95
}
96
+
97
+ /**
98
+ * Tests that not whitelisted elements should not be removed from DB to avoid backwards-incompatible change.
99
+ *
100
+ * @moduleName Magento_TestSetupDeclarationModule6
101
+ */
102
+ public function testDestructiveOperationBehaviour ()
103
+ {
104
+ $ this ->cliCommand ->install (['Magento_TestSetupDeclarationModule6 ' ]);
105
+ $ this ->assertForeignKeyPresence ('test_table ' , 'TEST_TABLE_TINYINT_REFERENCE_TABLE_TINYINT_REF ' );
106
+
107
+ $ this ->moduleManager ->updateRevision (
108
+ 'Magento_TestSetupDeclarationModule6 ' ,
109
+ 'remove_fk_declaration ' ,
110
+ 'db_schema.xml ' ,
111
+ 'etc '
112
+ );
113
+ $ this ->moduleManager ->updateRevision (
114
+ 'Magento_TestSetupDeclarationModule6 ' ,
115
+ 'remove_fk_declaration ' ,
116
+ 'db_schema_whitelist.json ' ,
117
+ 'etc '
118
+ );
119
+ $ this ->cliCommand ->upgrade ();
120
+ $ this ->assertForeignKeyPresence ('test_table ' , 'TEST_TABLE_TINYINT_REFERENCE_TABLE_TINYINT_REF ' );
121
+
122
+ $ this ->moduleManager ->updateRevision (
123
+ 'Magento_TestSetupDeclarationModule6 ' ,
124
+ 'restore_fk_declaration_to_wl ' ,
125
+ 'db_schema_whitelist.json ' ,
126
+ 'etc '
127
+ );
128
+ $ this ->cliCommand ->upgrade ();
129
+ $ this ->assertForeignKeyPresence ('test_table ' , 'TEST_TABLE_TINYINT_REFERENCE_TABLE_TINYINT_REF ' , false );
130
+ }
131
+
132
+ /**
133
+ * Asserts foreign key presence.
134
+ *
135
+ * @param string $tableName
136
+ * @param string $foreignKeyName
137
+ * @param bool $isPresent
138
+ * @return void
139
+ */
140
+ private function assertForeignKeyPresence (string $ tableName , string $ foreignKeyName , bool $ isPresent = true ): void
141
+ {
142
+ $ foreignKeys = $ this ->dbSchemaReader
143
+ ->readReferences ($ this ->resourceConnection ->getTableName ($ tableName ), 'default ' );
144
+ if ($ isPresent ) {
145
+ $ this ->assertArrayHasKey ($ foreignKeyName , $ foreignKeys );
146
+ } else {
147
+ $ this ->assertArrayNotHasKey ($ foreignKeyName , $ foreignKeys );
148
+ }
149
+ }
90
150
}
0 commit comments