Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix copying filters for e2t messages and conditional redirects. #122

Open
wants to merge 4 commits into
base: 7.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions campaignion_action/src/Redirects/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ class Filter extends Model {
* Data representing the filter.
*/
public static function fromArray(array $data) {
$data += ['id' => NULL, 'weight' => 0];
$data += ['weight' => 0];
$filter = new static();
$filter->id = $data['id'];
$filter->setData($data);
return $filter;
}
Expand Down
2 changes: 1 addition & 1 deletion campaignion_action/src/Redirects/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function setFilters(array $new_filters) {

foreach ($new_filters as $nf) {
if ($nf instanceof Filter) {
$f = $nf;
$f = $nf->redirect_id == $this->id ? $nf : clone $nf;
}
else {
// Reuse filter objects if 'id' is passed and found.
Expand Down
32 changes: 32 additions & 0 deletions campaignion_action/tests/Redirects/RedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,38 @@ public function testRedirectCloning() {
$this->assertEqual(1, $t1->filters[0]->config['value']);
}

/**
* Test copying a filter from one redirect template to another.
*/
public function testCopyFilter() {
$data = [
'id' => 42,
'label' => 'A',
'destination' => 'node/50',
'filters' => [['type' => 'test', 'value' => 1]],
];
$redirect_a = new Redirect($data);
$redirect_a->filters[0]->id = 42;

$data = [
'id' => 43,
'label' => 'B',
'destination' => 'node/50',
'filters' => [],
];
$redirect_b = new Redirect($data);

// Copy as array.
$redirect_b->setFilters([$redirect_a->filters[0]->toArray()]);
$this->assertEquals(42, $redirect_a->filters[0]->id);
$this->assertNotEquals(42, $redirect_b->filters[0]->id);

// Copy as filter object.
$redirect_b->setFilters([$redirect_a->filters[0]]);
$this->assertEquals(42, $redirect_a->filters[0]->id);
$this->assertNotEquals(42, $redirect_b->filters[0]->id);
}

/**
* Test checkFilters without any filters configured (ie. default redirect).
*/
Expand Down
1 change: 1 addition & 0 deletions campaignion_email_to_target/messages_app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"lodash.omit": "^4.5.0"
},
"devDependencies": {
"@vue/test-utils": "^1.0.0-beta.20",
"autoprefixer": "^6.7.2",
"babel-core": "^6.22.1",
"babel-eslint": "^7.1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ export default {
this.$bus.$on('duplicateSpec', index => {
const duplicate = clone(this.specs[index])
duplicate.id = emptySpec(duplicate.type).id
duplicate.filters.forEach((filter) => { filter.id = null })
duplicate.label = Drupal.t('Copy of @messageName', {'@messageName': duplicate.label})
this.currentSpec = duplicate
this.determineExclusionMode()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import mutations from '@/store/mutations'
import initialState from '@/store/state'
import testData from '../fixtures/example-data'
import Vue from 'vue'
import { shallowMount } from '@vue/test-utils'
import SpecDialog from '@/components/SpecDialog'

describe('components', function () {
var state

beforeEach(function () {
state = Object.assign({}, initialState)
mutations.initializeData(state, testData)
})

describe('SpecDialog', () => {
Vue.config.ignoredElements = [
'el-dialog', 'el-button', 'el-dropdown', 'el-dropdown-menu', 'el-dropdown-item'
]
const bus = new Vue()

it('duplicates a spec', () => {
const wrapper = shallowMount(SpecDialog, {
mocks: {
$store: {
state: state,
commit: function () {}
},
$bus: bus
}
})
var s = state.specs[0]
bus.$emit('duplicateSpec', 0)

expect(wrapper.vm.currentSpec.label).to.equal(`Copy of ${s.label}`)
expect(wrapper.vm.currentSpec.type).to.equal(s.type)
expect(wrapper.vm.currentSpec.message).to.deep.equal(s.message)
expect(wrapper.vm.currentSpec.id, 'id should be resetted').to.be.null
for (let filter of wrapper.vm.currentSpec.filters) {
expect(filter.id, 'filter ids should be resetted').to.be.null
}
})
})
})
4 changes: 3 additions & 1 deletion campaignion_email_to_target/src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class Filter extends Model {
public $config = [];

public static function fromArray($data) {
return new static($data);
$f = new static();
$f->setData($data);
return $f;
}

public function __construct($data = array(), $new = TRUE) {
Expand Down
2 changes: 1 addition & 1 deletion campaignion_email_to_target/src/MessageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function setFilters(array $new_filters) {

foreach ($new_filters as $nf) {
if ($nf instanceof Filter) {
$f = $nf;
$f = $nf->message_id == $this->id ? $nf : clone $nf;
}
else {
// Reuse filter objects if 'id' is passed and found.
Expand Down
35 changes: 35 additions & 0 deletions campaignion_email_to_target/tests/MessageTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,41 @@ public function testMessageCloning() {
$this->assertEqual(1, $t1->filters[0]->config['value']);
}

/**
* Test copying a filter from one message template to another.
*/
public function testCopyFilter() {
$data = [
'id' => 42,
'type' => 'message',
'label' => 'A',
'filters' => [[
'type' => 'test',
'config' => ['value' => 1],
]],
];
$template_a = new MessageTemplate($data);
$template_a->filters[0]->id = 42;

$data = [
'id' => 43,
'type' => 'message',
'label' => 'B',
'filters' => [],
];
$template_b = new MessageTemplate($data);

// Copy as array.
$template_b->setFilters([$template_a->filters[0]->toArray()]);
$this->assertEquals(42, $template_a->filters[0]->id);
$this->assertNotEquals(42, $template_b->filters[0]->id);

// Copy as filter object.
$template_b->setFilters([$template_a->filters[0]]);
$this->assertEquals(42, $template_a->filters[0]->id);
$this->assertNotEquals(42, $template_b->filters[0]->id);
}

/**
* Test creating a message instance.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ export default {
this.$root.$on('duplicateRedirect', index => {
const duplicate = clone(this.redirects[index])
duplicate.id = emptyRedirect().id
duplicate.filters.forEach((filter) => { filter.id = null })
duplicate.label = Drupal.t('Copy of @redirectLabel', {'@redirectLabel': duplicate.label})
this.currentRedirect = duplicate
this.$store.commit('editNewRedirect')
Expand Down