UKW-Frequenzen: Abschaltung vorerst abgewendet, aber Streit geht weiter

heise online Newsticker - 10. April 2018 - 17:30
Der Betreiber einer Sendeinfrastruktur hatte im Streit um Geld mit der Zwangsabschaltung mehrerer UKW-Sender gedroht. Nun hat sich das Unternehmen offenbar mit zwei Netzbetreibern auf einen Weiterbetrieb geeinigt - jedoch vorerst nur bis zur Jahresmitte.

Convertible-PC HP ZBook Studio x360 G5: 4K-Display mit 600 Lumen

heise online Newsticker - 10. April 2018 - 17:00
HP frischt die mobilen Workstations seiner ZBook-Serie auf. Der Convertible-PC Studio x360 G5 erhält ein 4K-Display, das mit 600 Lumen strahlt. Das größte Modell ZBook 17 kann bis zu 10 TByte Daten speichern.

150 Jahre TU München: Rückbesinnung der Technik auf die Gesellschaft gefordert

heise online Newsticker - 10. April 2018 - 17:00
Den Menschen muss die Angst vor den technischen Entwicklungen genommen werden, findet der Präsident der TU München. In der Forschung muss die Technik enger mit der Sozial- und Humanwissenschaft verbunden werden.

Acro Media: Automated Drupal Code Testing: What, Why, and When To Do It

Planet Drupal - 10. April 2018 - 16:45

Automated testing is like flossing your teeth: you know you should do it, you might even tell people you do it, but chances are you don't do it nearly as often or as consistently as you ought to. Maybe you only run one automated test. On five percent of your code. Sometimes.

Manual testing vs automated testing

Manual testing—where a real live person clicks around and verifies that the code does everything it's supposed to do—has its uses. But for large-scale projects, or in cases where you need to test the same code repeatedly, automated testing can be both more cost-effective and more fruitful. You know how your eye can look at the same spelling error six times without seeing it? Automated testing can catch issues like that. It's great for rote, boring tasks that humans gloss over.

In test-driven development, you actually build the tests first and then write the code that fulfills those tests. But you don't necessarily have to do automated testing that way. Tests can be written afterwards. Sometimes it's old code that gets automated tests built for it, for instance.

Writing the test probably takes more time than it takes to initially test it. But then it's done, and you can re-run the test any time you make any change to that site. Even if you don't change anything near that code, you can run the test anyways and catch those instances where you're like, "I'm sure it won't break that"…but it does.

Drupal maintainers try to be really strict about the automated tests. Since lots of people use the modules, and lots of people contribute to them, you have all these different people submitting code. Having a good test suite can really improve your confidence in a module. If each submission comes with new tests that you can run and verify, you can be far more confident in the quality of that code.

Time savings

Say you do 10 hours of manual testing for each release. If, on the other hand, you spend 10 hours writing automated tests for each release, then for release #2, you're actually doing 20 hours of testing, and for release #3 you're doing 30 hours of testing. You're only putting in 10 man hours each time, but your test suite keeps growing, and the scope of what you're testing increases exponentially because you can rerun the same tests each time.

Why is automated testing such a hard sell?

Going back to the flossing example: why don't you floss? It takes 60 seconds. But you only really get the benefits if you do it all the time.

More to the point: not doing it takes time to become bad. Skipping automated testing on your first release is maybe not a big deal. Your code base is small, it's probably only doing a couple things, so manual testing is very feasible. But as the project grows and gets more complex, manual testing becomes increasingly unwieldy, and then you get to a point where you think, we're too far into this to add automated testing now.

But the truth is you can start at any point. It's never too late to start proactively doing things that will benefit your site.

What prevents people from getting started?

Usually the thing that stymies people is that they're not set up for automated testing: they don't have a continuous integration environment or a nice testing environment to run the tests on. Or maybe they've neglected it for so long that the regular tests don't work anymore; they write their first test and they have all these other problems because they've let things languish, so they give up.

What is continuous integration, you ask? It means every time you do a change and you push it out, it attempts to integrate it in with the whole project. It will deploy it to a server, it will compile the code if necessary, it will run code standards and automated tests and so on. When you have that stuff all set up to run automatically, you just make the code, push it to your version control, and forget about it. If something goes south, it'll send you an email saying this didn't pass. Otherwise, you don't have to think about it again.

How much of your code should be covered by automated testing?

You kind of want to be in that 95% range, although it's true that you can have parts that aren't easily testable. You can cover a lot of code, but you might still be missing use cases. Maybe you test taxes, and you test discounts, but you don't test taxes and discounts together. So technically you have full code coverage, but you're not using them in combination. So code coverage is a nice metric, but it doesn't tell the whole story.

TLDR: Automated testing is like flossing. You should really do it.

More from Acro Media Chat with us

If you'd like to talk about adding automated testing to your Drupal Commerce website, or if you just want to to discuss how Drupal Commerce fits into your ecommerce solution, give us a shout. We're happy to chat.

Fabrik der Zukunft immer unabhängiger vom Standort

heise online Newsticker - 10. April 2018 - 16:30
Noch vernetzter, noch intelligenter: Die Fabrik der Zukunft wird smart. Doch damit könnte der Standort Deutschland an Bedeutung verlieren. Hannover-Messe-Chef Köckler fordert daher die "optimale Infrastruktur" für die digitale Zukunft.

Natur von romantisch bis skurril: Die Bilder der Woche (KW 14)

heise online Newsticker - 10. April 2018 - 16:00
Die Naturfotografie kennt viele spannende Facetten. Einige davon präsentieren wir hier mit den prämierten Bildern dieser Woche aus der Online-Galerie von c't Fotografie. Tutorial: showing BEE reservations on an event calendar

Planet Drupal - 10. April 2018 - 15:55
BEE makes it easy to quickly implement all kinds of booking & reservation use cases. We've created a new video that walks you through setting up an event calendar displaying BEE reservations.

Was war. Was wird. Vom Unrecht des Vergessens und anderen deutschen Befindlichkeiten

heise online Newsticker - 10. April 2018 - 15:30
Sie wollten vergessen, die rechtschaffenen Deutschen im Wirtschaftswunderland, und das ließen die 68er nicht zu, sagt Hal Faber 50 Jahre später. Nun sind die nächsten neuen Deutschen (wieder?) da und krakeelen zurück in die Zukunft.

Elektromobilität: Umweltministerin will Kaufprämie von 7000 Euro für E-Lieferwagen

heise online Newsticker - 10. April 2018 - 15:00
Mit einer nahezu verdoppelten Elektroauto-Prämie will Svenja Schulze E-Lieferwagen für Logistiker und Handwerker lohnenswert machen. Das soll die Luftqualität in Städten verbessern, denn eine rechtliche Handhabe für Diesel-Nachrüstungen gebe es nicht.

Studie: Roboter bedrohen Jobs in Deutschland stärker als in anderen Industrienationen

heise online Newsticker - 10. April 2018 - 14:30
Die Entwicklung von Robotern und Software kann in etwa 20 Jahren jeden fünften Arbeitnehmer in Deutschland ersetzen, besagt eine OECD-Studie. Deutschland ist stärker betroffen als die meisten anderen Mitgliedsstaaten.

Facebook-Datenskandal: Beraterfirma AggregateIQ ebenfalls suspendiert

heise online Newsticker - 10. April 2018 - 14:00
Nach Bekanntwerden des Skandals um Cambridge Analytica hat Facebook ein weiteres Unternehmen ausgesperrt. AggregateIQ war an der Kampagne der Brexit-Befürworter beteiligt und soll Verbindung zu CA gehabt haben.

Diesel-Skandal: EU-Kommission soll europaweite Sammelklagen vorbereiten

heise online Newsticker - 10. April 2018 - 14:00
Laut einem Zeitungsbericht will die EU-Kommission einen Vorstoß für europaweite Sammelklagen unternehmen. Damit könnten Verbände Schadenersatzklagen gegen Autohersteller einreichen. Das ginge weit über ein Vorhaben der Bundesregierung hinaus.

Astronomiekonferenz EWASS: Riesenteleskope und Neutronensternenstaub

heise online Newsticker - 10. April 2018 - 13:30
Die astronomische Forschung ist seit Galileo Galilei immer aufwendiger und komplizierter geworden. Neue und immer größere Teleskope bieten unzählige Möglichkeiten, trotzdem hat sich an der Entwicklung seit Galileis Zeiten nichts Grundlegendes geändert. Rendering Drupal 8 fields (the right way)

Planet Drupal - 10. April 2018 - 13:21

Once upon a time, we wrote an article about how to render fields on their own in Drupal 7, which was really handy because Drupal 7 wasn't always intuitive. It's common to want to display a field outside of the context of its main entity page, like showing author information in a sidebar block or in a panel, but you had to just know which functions to use. Drupal 8 has come along since then using 'grown up' things like objects and methods, which actually makes the job a little easier. So now we have this:

The short answer $node->field_my_thing->view();Quick reference

I'll cover these in detail below, but here are the things you might want to be doing:

  1. Render a field as if it were on the node/entity page (e.g. with the markup from the normal formatter and full field template)

Drupal 7: field_view_field(), optionally passing a view mode string, or formatter settings array.
Drupal 8: $node->field_my_thing->view(), optionally passing a view mode string, or formatter settings array.

  1. Just get a single value out of a field (i.e. raw values, usually as originally inputted)

Drupal 7: field_get_items() and then retrieve the index you want from the array.
Drupal 8: $node->field_my_thing->get(0)->value, passing just the index you want to get(). Properties other than 'value' may be available.

  1. Render a single value as if it were on the node/entity page (e.g. with the normal formatter's markup, but not all the wrappers that Drupal's field templates give you)

Drupal 7: field_view_value(), optionally passing a view mode string, or formatter settings array, but always passing the actual items array.
Drupal 8: $node->field_my_thing->get(0)->view(), passing just the index you want to get() and optionally passing a view mode string, or formatter settings array to view().

The long answer

Now that entities like nodes are properly classed objects, and fields use the fancy new Typed Data API, we don't need to care about the underlying data structure for nodes or their fields, we can just call the method to perform the operation we want! You know, just what methods are supposed to be for! You want to view a field? Just call its 'view' method.

The output will be automatically sanitised and goes through the normal formatter for the field, as well as the regular field template. You can specify whether you want it rendered as if it were in a teaser or other view mode, by passing in the view mode string, just as we did with field_view_field(). (Or you might have used something like $node->field_my_thing['und'][0]['value'] - in which case, go back and read our article for Drupal 7!)


Or even override the formatter to be used altogether (which is handy if the field would normally be hidden in any view mode):

$node->field_my_thing->view([ 'type' => 'image', 'label' => 'hidden', 'settings' => array( 'image_style' => 'larger_thumbnail', 'image_link' => 'content', ), ]);

This does assume that your field ('field_my_thing') in my examples does at least exist on your node (even if it's empty). You may want to wrap the whole code in a try/catch block, just in case it might not.

For bonus points, you could load up the normal formatter settings, and tweak them:

use Drupal\Core\Entity\Entity\EntityViewDisplay; // If you have the entity/node you want, use collectRenderDisplay() as it may // already be statically cached, plus it goes through various alter hooks. $display_options = EntityViewDisplay::collectRenderDisplay($node, 'teaser') ->getComponent('field_images'); // Otherwise - or if you intentionally want to re-use the settings from another // unrelated entity type, bundle or display mode - just load the display config. $display_options = EntityViewDisplay::load('pagaraph.media_pod.teaser') ->getComponent('field_images'); // Then tweak those display options and view the field. $display_options['settings']['image_style'] = 'even_bigger_thumbnail'; $node->field_my_thing->view($display_options);

This all assumes you've at least loaded your node, or other entity. (It works with any content entity, like terms, paragraphs, etc!) You'd probably be putting the result of the view() method (which will be a render array) into a variable to be used in a twig template via a preprocess hook. Or maybe you're just adding it into an existing render array, like a custom block plugin. (Either way, you probably shouldn't then be rendering it into a string yourself, let Drupal do that for you.)

You can even just view a single item within a multi-value field like this, here using an 'if' condition to be a bit more graceful than a try/catch. This is the equivalent of using field_view_value() from Drupal 7, so also skips Drupal's full field template, though includes markup produced by the field's formatter:

// View the third value, as long as there is one. if ($third = $node->field_my_thing->get(2)) { $output = $third->view(); } else { $output = []; }

That helps you see how you might get a single value too, with the get() method, though note that it still returns a classed object. To just get a raw value, without any wrapping markup or value processing/sanitisation, you might want to use something like this, instead of Drupal 7's field_get_items() :

$text = $node->field_my_thing->get(0)->value; // If the field is just a single-value field, you can omit the get() part. $value = $node->field_single_thing->value; // Some types of field use different properties. $url = $node->field_my_link->uri; // You can use getValue() to get all the properties (e.g. text value + format). $text_values = $node->field_formatted_text->getValue(); // References can be chained! $referenced_title = $node->field_my_reference->entity->field_other_thing->value;

In Drupal 7, there was also confusion around what to do for multilingual content. In Drupal 8, as long as you've got the translation you want first, all the methods I've discussed above will get you the appropriate values for your language. To get a specific translation, use:

if ($node->hasTranslation($candidate)) { $node = $node->getTranslation($langcode); } This Rocks.

You get to use proper modern methods on a proper typed data API. Sanitisation is done for you. You don't need to care what the underlying data structure is. And you don't need to remember some magic global procedural functions, because the methods are obvious, right there on the thing you want to use them on (the field item class). If the Drupal 7 version of this was brilliant, that makes the Drupal 8 version of this even better. Brilliant-er?

Facebook plant weitere Einschränkungen für Polit-Werbung

heise online Newsticker - 10. April 2018 - 12:30
Wer bei dem sozialen Netzwerk Werbung mit politischem Inhalt schalten will, muss künftig strengere Regeln einhalten und unter anderem seine Identität offenlegen. Mark Zuckerberg hofft, dass sich so bei anstehenden Wahlen Manipulationen verhindern lassen.

Fette 4K-Fernseher: UHD Smart TVs unter 500 Euro

heise online Newsticker - 10. April 2018 - 12:30
Die Preise für 4K UHD Fernseher sind im freien Fall. Inzwischen gibt es dutzende Geräte unter 500 Euro, die bei Bildschirmdiagonalen über einem Meter Inhalte in höchster Auflösung darstellen. TechStage zeigt interessante Produkte und gibt einen Überblick.

Wochenrückblick Replay: Chrome, iOS 11.3, Musks Schlaflager

heise online Newsticker - 10. April 2018 - 12:30
Chrome scannt Dateien auf dem System, Trump schießt gegen Amazon, ältere Intel-Prozessoren bleiben ungeschützt: Die wichtigsten Meldungen der vergangenen Woche.

c't uplink 21.7: Outdoor-Smartphones, Backup-Software für Windows

heise online Newsticker - 10. April 2018 - 12:00
Heute in c't uplink: Robuste Smartphones und Tablets mit langen Laufzeiten und Backups für Windows 10.

Facebook wollte Zugriff auf US-Patientendaten

heise online Newsticker - 10. April 2018 - 12:00
Facebook hatte US-Krankenhäuser um anonymisierte Patientendaten gebeten. Daraus sollten Profile zur Verbesserung der medizinischen Versorgung erzeugt werden, erklärte das soziale Netzwerk.

Diesel-Skandal: Bundesregierung erwägt angeblich Hardware-Nachrüstung bei Diesel-Autos

heise online Newsticker - 10. April 2018 - 12:00
In der Bundesregierung wird überlegt, Diesel-Autos mit SCR-Katalysatoren nachrüsten zu lassen, berichtet "Der Spiegel". Dabei sollen zunächst Autos aus bestimmten Regionen drankommen.