Spain has been a model of solidarity in the three days since the terror attacks that killed 14 people in Barcelona and Cabrils. That number is now known to include the seven-year-old Julian Cadman, who had dual British-Australian nationality and whose engaging image has been on front pages. He had been missing since the savage vehicle attack on Thursday afternoon. On Sunday morning the king and queen led the mourners at a service in La Sagrada Familia, Barcelona’s basilica, which, perhaps paradoxically in the circumstances, was conceived by Antoni Gaudí as a paeon to faith and nationalism. More than 1,500 people packed the church, while nearby Las Ramblas continued to be a focus for grief and resistance.

But behind the solidarity, Spain’s national cohesion faces more stresses than in most European countries. At least eight of the terrorists appear to have grown up in one small town, Ripoll. Their horrified families are blaming Abdelbaki Es Satty, the imam of one of the town’s mosques, for radicalising their sons. The small community, where one in 10 residents is a migrant, is in a state of shock to discover that football-loving kids who appeared entirely comfortable with their Spanish identity set out on such a murderous course. Police, who are investigating what they now say was a plot to launch a huge terror attack, are trying to establish whether the imam died in a gas explosion that destroyed a house last Wednesday.

Yet this is a region that is uncomfortably familiar with conflicts of identity. Catalonia’s president, Carles Puigdemont, attended the Sunday service with the king and queen, but it was a rare joint appearance with the monarchs for the republican politician, who is the architect of the unofficial referendum on independence that is scheduled to take place in less than six weeks’ time. Madrid continues to insist the plebiscite is illegal and that it will do everything it can to stop it happening. The last Catalan president who organised a similar referendum has been banned from public office for two years.

Thursday’s attack is likely to be used by Madrid to add to the pressure on the regional government. The argument has already been rehearsed in the national capital’s newspaper El País, which said it was a wake-up call for the Catalan government, described independence as a fantasy and called on the regional politicians to “ditch the democratic nonsense … and start working for our real interests”.

There are plenty of people, not just in the darker corners of social media, who believe there is a link between terror and refugees. Late last year, Europol reported Islamic State was deliberately trying to radicalise vulnerable refugees in order to inflame the migration crisis and turn EU citizens against refugees seeking asylum. That makes the challenge Barcelona faces now, to sustain the qualities of a multicultural cosmopolitanism, the youthful and open approach that has made it so beloved, the more important. It has a fine record: its recent hostility to rapidly growing tourism, partly aggravated by the way Airbnb has driven up rents in the city, is more than matched – as banners during anti-tourism protests showed – by its readiness to welcome refugees. It describes itself as a “refugee city” and it has an impressive plan for comprehensive reception and resettlement facilities. But Barcelona’s offer of welcome is not shared absolutely everywhere in Spain, and, last week, the UN refugee agency warned the country was struggling to cope with the 9,300 refugees who have already arrived this year. That number will only increase as the route from Libya becomes ever more dangerous and some of the tens of thousands who risk the crossing to Italy seek a safer route.

The past year has seen terror strike half a dozen European countries. Events in Spain were still unfolding when a man went on the rampage in the Finnish town of Turku, murdering two women. His ethnicity, like that of the Spanish terrorists, was Moroccan. These brutal murders have played a part in poisoning the atmosphere against migrants. While Angela Merkel of Germany stoutly upholds her commitment to refugees, the EU collectively has failed at every turn, unable even to ensure that members honour the commitments they have already made to ease the burden on the frontline states of Greece and Italy. This is a desperate tragedy for millions fleeing endemic violence and insecurity. It threatens, too, the precious cohesion of neighbourhoods. And it is a kind of victory for the terrorists of all sides who seek to undermine Europe’s universal values.

• This article was amended on 22 August 2017. An earlier version said La Sagrada Familia was a cathedral. This has been corrected to basilica.