Skip to content

Instantly share code, notes, and snippets.

@ygerasimov
Created December 3, 2019 16:32
Show Gist options
  • Save ygerasimov/f24315a07d3ce0d4baf302821ba88795 to your computer and use it in GitHub Desktop.
Save ygerasimov/f24315a07d3ce0d4baf302821ba88795 to your computer and use it in GitHub Desktop.
Attempt to have a BrowserTest based on installing a profile from existing configuration
<?php
namespace Drupal\Tests\mymodule_core\Unit;
use CommerceGuys\Addressing\AddressFormat\AddressFormatRepository;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Database\Database;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Installer\Form\SelectProfileForm;
use Drupal\Core\Site\Settings;
use Drupal\mymodule_order\Entity\Order;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\UnitTestCase;
use CommerceGuys\Addressing\Address;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request;
/**
* @coversDefaultClass \Drupal\mymodule_order\Entity\Order
* @group mymodule
*/
class OrderAddressLabelsTest extends BrowserTestBase {
// protected $profile = SelectProfileForm::CONFIG_INSTALL_PROFILE_KEY;
// protected $profile = 'config_installer';
public static $modules = ['address', 'mymodule_core', 'mymodule_order'];
const ADDRESS_CASES = [
[
'address' => [
'country_code' => 'US',
'administrative_area' => 'CA',
'locality' => 'Santa Clara',
'dependent_locality' => '',
'postal_code' => '85051',
'address_line1' => '5409 Stevens Creek Blvd - M/S 318-5ST',
'address_line2' => '',
'organization' => 'Apple Inc.',
],
'export' => [
'delivery_street_address' => '5409 Stevens Creek Blvd - M/S 318-5ST',
'delivery_street_address2' => '',
'delivery_city_name' => 'Santa Clara',
'delivery_state_province' => 'CA',
'delivery_postal_code' => '85051',
'delivery_country' => 'US',
],
],
];
/**
* Create addresses from different countries and see how they get exported for Partner Connect.
*/
public function testAddressesExport() {
foreach (self::ADDRESS_CASES as $case) {
$address = new Address(
$case['address']['country_code'],
$case['address']['administrative_area'],
$case['address']['locality'],
$case['address']['dependent_locality'],
$case['address']['postal_code'],
'', // Sorting code.
$case['address']['address_line1'],
$case['address']['address_line2'],
'', // Organization.
'', // Given name.
'', // Additional name.
'' // Family name.
);
$export = Order::submitAddressFields($address);
$this->assertArrayEquals($export, $case['export']);
}
}
/**
* Asserts if two arrays are equal by sorting them first.
*
* @param array $expected
* @param array $actual
* @param string $message
*/
protected function assertArrayEquals(array $expected, array $actual, $message = NULL) {
ksort($expected);
ksort($actual);
$this->assertEquals($expected, $actual, $message);
}
// protected function setUpSite() {
// // Recreate the container so that we can simulate the submission of the
// // SyncConfigureForm after the full bootstrap has occurred. Without out this
// // drupal_realpath() does not work so uploading files through
// // WebTestBase::postForm() is impossible.
// $request = Request::createFromGlobals();
// $class_loader = require $this->container->get('app.root') . '/vendor/autoload.php';
// Settings::initialize($this->container->get('app.root'), DrupalKernel::findSitePath($request), $class_loader);
// foreach ($GLOBALS['config_directories'] as $type => $path) {
// $this->configDirectories[$type] = $path;
// }
// $this->kernel = DrupalKernel::createFromRequest($request, $class_loader, 'prod', FALSE);
// $this->kernel->prepareLegacyRequest($request);
// $this->container = $this->kernel->getContainer();
//
// $this->setUpSyncForm();
// $this->setUpInstallConfigureForm();
// // If we've got to this point the site is installed using the regular
// // installation workflow.
// $this->isInstalled = TRUE;
// }
//
// protected function setUpInstallConfigureForm() {
// $params = $this->parameters['forms']['install_configure_form'];
// unset($params['site_name']);
// unset($params['site_mail']);
// unset($params['update_status_module']);
// $edit = $this->translatePostValues($params);
// $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
// }
//
// protected function setUpSyncForm() {
// // Create a new sync directory.
//// drupal_mkdir($this->syncDir);
////
//// // Extract the tarball into the sync directory.
//// $this->extractTarball($this->getTarball(), $this->syncDir);
//
// $this->drupalPostForm(NULL, [
// 'sync_directory' => '/var/www/html/config/sync'
// ], 'Save and continue');
// }
// protected function installParameters() {
// $connection_info = Database::getConnectionInfo();
// $driver = $connection_info['default']['driver'];
// $connection_info['default']['prefix'] = $connection_info['default']['prefix']['default'];
// unset($connection_info['default']['driver']);
// unset($connection_info['default']['namespace']);
// unset($connection_info['default']['pdo']);
// unset($connection_info['default']['init_commands']);
// // Remove database connection info that is not used by SQLite.
// if ($driver === 'sqlite') {
// unset($connection_info['default']['username']);
// unset($connection_info['default']['password']);
// unset($connection_info['default']['host']);
// unset($connection_info['default']['port']);
// }
// $parameters = [
// 'interactive' => FALSE,
// 'parameters' => [
// 'profile' => $this->profile,
// 'langcode' => 'en',
// ],
// 'forms' => [
// 'install_settings_form' => [
// 'driver' => $driver,
// $driver => $connection_info['default'],
// ],
// 'config_installer_sync_configure_form' => [
// 'sync_directory' => '/var/www/html/config/sync',
// ],
// 'install_configure_form' => [
// 'site_name' => 'Drupal',
// 'site_mail' => 'simpletest@example.com',
// 'account' => [
// 'name' => $this->rootUser->name,
// 'mail' => $this->rootUser->getEmail(),
// 'pass' => [
// 'pass1' => isset($this->rootUser->pass_raw) ? $this->rootUser->pass_raw : $this->rootUser->passRaw,
// 'pass2' => isset($this->rootUser->pass_raw) ? $this->rootUser->pass_raw : $this->rootUser->passRaw,
// ],
// ],
// // form_type_checkboxes_value() requires NULL instead of FALSE values
// // for programmatic form submissions to disable a checkbox.
// 'enable_update_status_module' => NULL,
// 'enable_update_status_emails' => NULL,
// ],
// ],
// ];
//
// // If we only have one db driver available, we cannot set the driver.
// include_once DRUPAL_ROOT . '/core/includes/install.inc';
// if (count($this->getDatabaseTypes()) == 1) {
// unset($parameters['forms']['install_settings_form']['driver']);
// }
// return $parameters;
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment