Problem
While trying to send a newsletter that is attached to the node, I get a huge list of notices telling that effort to get a property of non-object failed.
The symptoms are similar to #1739312: simplenews_send_source() results in PHP notices ("Trying to get property of non-object"), but definitely have another root, because the problem persists even in the latest dev.

Reason
That is so because I am trying to send a node, that existed before Simplenews module installation.

Solution
We need to create newsletter for each node existed before.

I will provide a patch soon.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

PatchRanger’s picture

Status: Active » Needs review
FileSize
4.06 KB

Please review the patch.

Berdir’s picture

Status: Needs review » Needs work
+++ b/simplenews.moduleundefined
@@ -696,6 +700,32 @@ function simplenews_get_category_field($bundle_name) {
+  // Construct a query of all nodes of this content type
+  // minus all simplenews newsletters.
+  $query = db_select('node');
+  $query->condition('type', $type->type);
+  $query->leftJoin('simplenews_newsletter', 'sn', 'node.nid = sn.nid');
+  $query->where('sn.nid IS NULL');
+  $query->fields('node', array('nid'));
+  // Get an array of nids.
+  $simplenews_nodes = $query->execute()->fetchCol();
+  // Turn the array of nids into an array of node objects.
+  $simplenews_nodes = node_load_multiple($simplenews_nodes);
+  // For each node from this array save newsletter.
+  foreach ($simplenews_nodes as $simplenews_node) {
+    $simplenews_newsletter = (object) simplenews_newsletter_defaults($simplenews_node);
+    simplenews_newsletter_save($simplenews_newsletter);

This does not scale. Imagine this is done on a node type with 10k or 100k nodes.

Instead, we should probably default to an empty/default newsletter object in case a node does not explicitly have one. We probable removed that due to refactoring but didn't think of that use case. To avoid this from happening again, we need to have tests for this. This will require a new test class which does not enable simplenews.module in setUp() but first creates a node, then uses module_enable() to enable simplenews and then attempt to send the newsletter.

PatchRanger’s picture

Status: Needs work » Needs review
FileSize
24.11 KB

@Berdir Thank you for detailed instructions.
I have created simpletest for this testcase - let's see how it will pass or not (in fact it should not).
Please review - make sure that this testcase really checks what we need to be checked.
It is my first simpletest ever created - so please be patient :)
Btw, I tried to follow your instructions as I have understood them (probably I have not) - as a result I had to do some refactoring of old testcases, hope you will appreciate it.

Status: Needs review » Needs work

The last submitted patch, simlenews_added_new_testcase.patch, failed testing.

PatchRanger’s picture

Status: Needs work » Needs review
FileSize
24.19 KB

Hoorah! I didn't break anything!))
But it is time to make new testcase to work as designed.
Please review this one.

Status: Needs review » Needs work

The last submitted patch, simplenews-added_new_testcase-1780134-5.patch, failed testing.

PatchRanger’s picture

Status: Needs work » Needs review
FileSize
24.2 KB

Ok, one more try.

Status: Needs review » Needs work

The last submitted patch, simplenews-added_new_testcase-1780134-7.patch, failed testing.

PatchRanger’s picture

Status: Needs work » Needs review
FileSize
24.61 KB

Number of fails and exceptions is decreasing - but is not zero yet.
Somewhen it will.

Status: Needs review » Needs work

The last submitted patch, simplenews-added_new_testcase-1780134-9.patch, failed testing.

PatchRanger’s picture

Status: Needs work » Needs review
FileSize
24.78 KB

Is it normal that I'm debugging here - or there is a way to do it locally?

Status: Needs review » Needs work

The last submitted patch, simplenews-added_new_testcase-1780134-11.patch, failed testing.

PatchRanger’s picture

Status: Needs work » Needs review
Berdir’s picture

You can execute the tests locally, just enable Simpletest and then run the tests that failed on the testbot.

I haven't looked at the patch yet, but note that there can't be any API changes in 7.x-1.x as that version is stable now. So you either need to do it without changing the API or it can only happen in 7.x-2.x. Where #1309404: Convert Newsletter categories to custom entity is changing *a lot* and will be commited first. It might even partially solve this as a side effect.

Status: Needs review » Needs work

The last submitted patch, simplenews-added_new_testcase-1780134-11.patch, failed testing.

PatchRanger’s picture

Oh, thanks, you are just in time.
This patch contains only new testcase that you were talking about. No API changes, just some remaking of TestCase-classes.
It seems that I'm done with tests: now it's time to make the code that passing them.

Where #1309404: Convert Newsletter categories to custom entity is changing *a lot* and will be commited first. It might even partially solve this as a side effect.

Tests are necessary here in any case.

Berdir’s picture

Looking at the patch, the problem is that you're are changing *a lot* that is not relevant to this issue in any way, like removing t()'s. See http://webchick.net/please-stop-eating-baby-kittens why this is a problem.

For example, a lot of your changes are going to conflict unecessarly with the linked issue, which means that you will have to re-do it for 7.x-2.x.

Also, changing the structure of tests is IMHO an API change too. Someone might have written a test (maybe in another contrib module) that relies on e.g. the simplenews test base class. A way to do it as an API *addition* instead of a change would e.g. be a second, optional argument that would not add simplenews. Or just not depend on the base class and copy some code instead.

PatchRanger’s picture

See http://webchick.net/please-stop-eating-baby-kittens why this is a problem.

I see, thanks for your patience.

A way to do it as an API *addition* instead of a change would e.g. be a second, optional argument that would not add simplenews. Or just not depend on the base class and copy some code instead.

I am going to think about it for a while. For now I tend to re-roll it against 7.x-2.x (of course, splitting it to more relevant parts).

PatchRanger’s picture

Issue summary: View changes

Fixed typo.