Skip to content

Commit ae1c58b

Browse files
author
Bomko, Alex(abomko)
committed
Merge pull request #356 from magento-nord/MAGETWO-34125-2
[NORD] MAGETWO-34125 Sample Data fix
2 parents acb837d + ec7c181 commit ae1c58b

File tree

15 files changed

+178
-9
lines changed

15 files changed

+178
-9
lines changed

setup/pub/magento/setup/customize-your-store.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ angular.module('customize-your-store', ['ngStorage', 'ngSanitize'])
1111
currency: 'USD',
1212
language: 'en_US',
1313
useSampleData: false,
14+
cleanUpDatabase: false,
1415
loadedAllModules: false,
1516
showModulesControl: false,
1617
selectAll: true,

setup/pub/magento/setup/install.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ angular.module('install', ['ngStorage'])
1010
$scope.isInProgress = false;
1111
$scope.isConsole = false;
1212
$scope.isDisabled = false;
13+
$scope.isSampleDataError = false;
14+
$scope.isShowCleanUpBox = false;
1315
$scope.toggleConsole = function () {
1416
$scope.isConsole = $scope.isConsole === false;
1517
};
@@ -34,6 +36,9 @@ angular.module('install', ['ngStorage'])
3436
$scope.progressText = response.data.progress + '%';
3537
} else {
3638
$scope.displayFailure();
39+
if (response.data.isSampleDataError) {
40+
$scope.isSampleDataError = true;
41+
}
3742
}
3843
if ($scope.isInProgress) {
3944
$timeout(function() {
@@ -43,7 +48,24 @@ angular.module('install', ['ngStorage'])
4348
});
4449
};
4550

51+
$scope.showCleanUpBox = function() {
52+
$scope.isShowCleanUpBox = true;
53+
};
54+
$scope.hideCleanUpBox = function() {
55+
$scope.isShowCleanUpBox = false;
56+
};
57+
$scope.startCleanup = function(performClenup) {
58+
$scope.hideCleanUpBox();
59+
$scope.isSampleDataError = false;
60+
$localStorage.store.cleanUpDatabase = performClenup;
61+
$scope.start();
62+
};
63+
4664
$scope.start = function () {
65+
if ($scope.isSampleDataError) {
66+
$scope.showCleanUpBox();
67+
return;
68+
}
4769
var data = {
4870
'db': $localStorage.db,
4971
'admin': $localStorage.admin,
@@ -60,6 +82,9 @@ angular.module('install', ['ngStorage'])
6082
$scope.nextState();
6183
} else {
6284
$scope.displayFailure();
85+
if (response.isSampleDataError) {
86+
$scope.isSampleDataError = true;
87+
}
6388
}
6489
});
6590
progress.get(function () {

setup/pub/styles/setup.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

setup/src/Magento/Setup/Controller/CustomizeYourStore.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public function indexAction()
4343
'currency' => $this->list->getCurrencyList(),
4444
'language' => $this->list->getLocaleList(),
4545
'isSampledataEnabled' => $this->sampleData->isDeployed(),
46+
'isSampleDataInstalled' => $this->sampleData->isInstalledSuccessfully(),
47+
'isSampleDataErrorInstallation' => $this->sampleData->isInstallationError()
4648
]);
4749
$view->setTerminal(true);
4850
return $view;

setup/src/Magento/Setup/Controller/Install.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ public function startAction()
9494
} catch (\Exception $e) {
9595
$this->log->logError($e);
9696
$json->setVariable('success', false);
97+
if ($e instanceof \Magento\Setup\SampleDataException) {
98+
$json->setVariable('isSampleDataError', true);
99+
}
97100
}
98101
return $json;
99102
}
@@ -107,15 +110,19 @@ public function progressAction()
107110
{
108111
$percent = 0;
109112
$success = false;
113+
$json = new JsonModel();
110114
try {
111115
$progress = $this->progressFactory->createFromLog($this->log);
112116
$percent = sprintf('%d', $progress->getRatio() * 100);
113117
$success = true;
114118
$contents = $this->log->get();
115119
} catch (\Exception $e) {
116120
$contents = [(string)$e];
121+
if ($e instanceof \Magento\Setup\SampleDataException) {
122+
$json->setVariable('isSampleDataError', true);
123+
}
117124
}
118-
return new JsonModel(['progress' => $percent, 'success' => $success, 'console' => $contents]);
125+
return $json->setVariables(['progress' => $percent, 'success' => $success, 'console' => $contents]);
119126
}
120127

121128
/**
@@ -178,6 +185,8 @@ private function importUserConfigForm()
178185
? $source['store']['currency'] : '';
179186
$result[InstallCommand::INPUT_KEY_USE_SAMPLE_DATA] = isset($source['store']['useSampleData'])
180187
? $source['store']['useSampleData'] : '';
188+
$result[InstallCommand::INPUT_KEY_CLEANUP_DB] = isset($source['store']['cleanUpDatabase'])
189+
? $source['store']['cleanUpDatabase'] : '';
181190
return $result;
182191
}
183192

setup/src/Magento/Setup/Model/Installer.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,14 +1102,22 @@ private function assertDbAccessible()
11021102
*
11031103
* @param array $request
11041104
* @return void
1105+
* @throws \Magento\Setup\SampleDataException
11051106
*
11061107
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) Called by install() via callback.
11071108
*/
11081109
private function installSampleData($request)
11091110
{
1110-
$userName = isset($request[AdminAccount::KEY_USER]) ? $request[AdminAccount::KEY_USER] : '';
1111-
$this->objectManagerProvider->reset();
1112-
$this->sampleData->install($this->objectManagerProvider->get(), $this->log, $userName);
1111+
try {
1112+
$userName = isset($request[AdminAccount::KEY_USER]) ? $request[AdminAccount::KEY_USER] : '';
1113+
$this->objectManagerProvider->reset();
1114+
$this->sampleData->install($this->objectManagerProvider->get(), $this->log, $userName);
1115+
} catch (\Exception $e) {
1116+
throw new \Magento\Setup\SampleDataException(
1117+
"Error during sample data installation: {$e->getMessage()}",
1118+
$e->getCode()
1119+
);
1120+
}
11131121
}
11141122

11151123
/**

setup/src/Magento/Setup/Model/SampleData.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,47 @@ public function isDeployed()
4747
return file_exists($this->directoryList->getPath(DirectoryList::MODULES) . self::PATH);
4848
}
4949

50+
/**
51+
* Get state object or null if state object cannot be initialized
52+
*
53+
* @return null|\Magento\SampleData\Helper\State
54+
*/
55+
protected function getState()
56+
{
57+
if ($this->isDeployed() && class_exists('Magento\SampleData\Helper\State')) {
58+
return new \Magento\SampleData\Helper\State();
59+
}
60+
return null;
61+
}
62+
63+
/**
64+
* Check whether installation of sample data was successful
65+
*
66+
* @return bool
67+
*/
68+
public function isInstalledSuccessfully()
69+
{
70+
$state = $this->getState();
71+
if (!$state) {
72+
return false;
73+
}
74+
return \Magento\SampleData\Helper\State::STATE_FINISHED == $state->getState();
75+
}
76+
77+
/**
78+
* Check whether there was unsuccessful attempt to install Sample data
79+
*
80+
* @return bool
81+
*/
82+
public function isInstallationError()
83+
{
84+
$state = $this->getState();
85+
if (!$state) {
86+
return false;
87+
}
88+
return \Magento\SampleData\Helper\State::STATE_STARTED == $state->getState();
89+
}
90+
5091
/**
5192
* Installation routine for creating sample data
5293
*
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Setup;
8+
9+
class SampleDataException extends \Exception
10+
{
11+
}

setup/src/Magento/Setup/Test/Unit/Controller/CustomizeYourStoreTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public function setup()
4040
public function testIndexAction($expected)
4141
{
4242
$this->sampleData->expects($this->once())->method('isDeployed')->willReturn($expected['isSampledataEnabled']);
43+
$this->sampleData->expects($this->once())->method('isInstalledSuccessfully')
44+
->willReturn($expected['isSampleDataInstalled']);
45+
$this->sampleData->expects($this->once())->method('isInstallationError')
46+
->willReturn($expected['isSampleDataErrorInstallation']);
4347
$this->lists->expects($this->once())->method('getTimezoneList')->willReturn($expected['timezone']);
4448
$this->lists->expects($this->once())->method('getCurrencyList')->willReturn($expected['currency']);
4549
$this->lists->expects($this->once())->method('getLocaleList')->willReturn($expected['language']);
@@ -64,8 +68,13 @@ public function indexActionDataProvider()
6468
$timezones = ['timezone' => ['America/New_York'=>'EST', 'America/Chicago' => 'CST']];
6569
$currency = ['currency' => ['USD'=>'US Dollar', 'EUR' => 'Euro']];
6670
$language = ['language' => ['en_US'=>'English (USA)', 'en_UK' => 'English (UK)']];
67-
$sampleDataTrue = ['isSampledataEnabled' => true];
68-
$sampleDataFalse = ['isSampledataEnabled' => false];
71+
$sampleData = [
72+
'isSampledataEnabled' => null,
73+
'isSampleDataInstalled' => null,
74+
'isSampleDataErrorInstallation' => null
75+
];
76+
$sampleDataTrue = array_merge($sampleData, ['isSampledataEnabled' => true]);
77+
$sampleDataFalse = array_merge($sampleData, ['isSampledataEnabled' => false]);
6978

7079
return [
7180
'with_all_data' => [array_merge($timezones, $currency, $language, $sampleDataTrue)],
@@ -76,7 +85,7 @@ public function indexActionDataProvider()
7685
'empty_timezone_data' => [array_merge(['timezone' => []], $currency, $language, $sampleDataTrue)],
7786
'empty_language_data' => [array_merge($timezones, $currency, ['language' => []], $sampleDataTrue)],
7887
'false_sample_data' => [array_merge($timezones, $currency, $language, $sampleDataFalse)],
79-
'no_sample_data' => [array_merge($timezones, $currency, $language, ['isSampledataEnabled' => null])],
88+
'no_sample_data' => [array_merge($timezones, $currency, $language, $sampleData)],
8089
];
8190
}
8291

setup/view/magento/setup/customize-your-store.phtml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@
4343
<label class="form-label" for="useSampleData">
4444
Use Sample Data
4545
</label>
46+
47+
<?php if ($this->isSampleDataInstalled || $this->isSampleDataErrorInstallation): ?>
48+
<div class="customize-database-clean">
49+
<p>
50+
<?php if ($this->isSampleDataInstalled): ?>
51+
You have already installed sample data. If you want to re-install it, your database has to be cleaned up
52+
<?php endif; ?>
53+
<?php if ($this->isSampleDataErrorInstallation): ?>
54+
Your sample data is broken. If you want to re-install it, your database has to be cleaned up
55+
<?php endif; ?>
56+
</p>
57+
<input
58+
type="checkbox"
59+
ng-model="store.cleanUpDatabase"
60+
class="form-el-checkbox"
61+
id="cleanUpDatabase"
62+
>
63+
<label class="form-label" for="cleanUpDatabase">
64+
Clean up automatically
65+
</label>
66+
</div>
67+
<?php endif; ?>
4668
</div>
4769
</div>
4870

setup/view/magento/setup/install.phtml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,28 @@
7474
<div class="console" ng-bind-html="log"></div>
7575
</div>
7676
</div>
77+
78+
<div class="install-database-clean" ng-show="isShowCleanUpBox">
79+
<p>To install sample data you should clean up you database</p>
80+
<button
81+
type="button"
82+
class="btn btn-secondary"
83+
ng-disabled="isDisabled"
84+
ng-click="startCleanup(true)">
85+
Clean up automatically
86+
</button>
87+
<button
88+
type="button"
89+
class="btn btn-secondary"
90+
ng-disabled="isDisabled"
91+
ng-click="startCleanup(false)">
92+
Proceed without clean up
93+
</button>
94+
<button
95+
type="button"
96+
class="btn"
97+
ng-disabled="isDisabled"
98+
ng-click="hideCleanUpBox()">
99+
Cancel
100+
</button>
101+
</div>

setup/view/styles/components/_navigation-bar.less

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
float: right;
5555
margin-left: .5rem;
5656
margin-right: .5rem;
57+
.btn {
58+
padding-left: .5rem;
59+
padding-right: .5rem;
60+
}
5761
}
5862
}
5963

setup/view/styles/lib/_buttons.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
display: inline-block;
4949
font-size: @btn__base__font-size;
5050
font-weight: @font-weight__semibold;
51-
padding: @btn__base__padding-top .5em;
51+
padding: @btn__base__padding-top .9em;
5252
text-align: center;
5353
&:hover {
5454
background-color: @btn__base__hover__background-color;

setup/view/styles/pages/_customize-your-store.less

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@
3131
}
3232
}
3333
}
34+
.customize-database-clean {
35+
p {
36+
margin-top: @indent__m;
37+
}
38+
}
3439
}

setup/view/styles/pages/_install.less

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,10 @@
5555
top: .15em;
5656
}
5757
}
58+
59+
.install-database-clean {
60+
margin-top: @indent__xl;
61+
.btn {
62+
margin-right: @indent__s;
63+
}
64+
}

0 commit comments

Comments
 (0)